XF 2.0 Display entities according to date with multiple pages

Matt C.

Well-known member
I've discovered a problem with an add-on I've developed. When I list entities on a page, it lists them newest to oldest on the first page only, BUT when you go to the next page, but it doesn't do that for the other pages.

You can see what I'm talking about if you visit the page on my site: https://anthemhub.net/dev-tracker/. Scroll down and go to the second page.

Here is my actionIndex on the my public controller:
Code:
public function actionIndex(ParameterBag $params)
{
    if ($params->entry_id)
    {
        return $this->actionEntry($params);
    }

    $page = $this->filterPage();
    $perPage = $this->options()->ahDevEntriesPerPage;
    $entries = $this->getEntryRepo()->findEntriesForList()->limitByPage($page, $perPage);
    $total = $entries->total();

    $devs = $this->getDevRepo()->findDevsForList();

    $this->entrySaveProcess();

    $viewParams = [
        'entries' => $entries,
        'devs' => $devs,

        'page' => $page,
        'perPage' => $perPage,
        'total' => $total,

    ];

    return $this->view('AH\DevTracker:DevTracker\View', 'ah_dev_tracker_index', $viewParams);
}

You can see this part ( $entries = $this->getEntryRepo()->findEntriesForList()->limitByPage($page, $perPage);), where I get my entities and limit them by page.

In my repository, I have this function, which finds the entities and orders them by the highest timestamp. Each entity has a timestamp property with their UNIX timestamp. I read on Stack Overflow that if you want to display the newest item, you need to set the order to descending.
PHP:
public function findEntriesForList()
{
  return $this->finder('AH\DevTracker:Entry')->order('timestamp', 'DESC');
}

Any help would be appreciated, thank you.
 
First: I would not recommend to have debug mode enabled on a public site!

Second: You do hundreds of SELECTs, see: https://anthemhub.net/dev-tracker/?_debug=1 - get rid of the ones that look like:
SQL:
SELECT `xf_ah_dev_tracker_entry`.*
FROM `xf_ah_dev_tracker_entry`

WHERE (`xf_ah_dev_tracker_entry`.`entry_id` = 'blgnoqka')


LIMIT 1

Third: Your SELECT seems ok. On page 1:
SQL:
SELECT `xf_ah_dev_tracker_entry`.*
FROM `xf_ah_dev_tracker_entry`


ORDER BY `xf_ah_dev_tracker_entry`.`timestamp` DESC

LIMIT 49
And on page 2:
SQL:
SELECT `xf_ah_dev_tracker_entry`.*
FROM `xf_ah_dev_tracker_entry`


ORDER BY `xf_ah_dev_tracker_entry`.`timestamp` DESC

LIMIT 49 OFFSET 49
 
First: I would not recommend to have debug mode enabled on a public site!

Second: You do hundreds of SELECTs, see: https://anthemhub.net/dev-tracker/?_debug=1 - get rid of the ones that look like:
SQL:
SELECT `xf_ah_dev_tracker_entry`.*
FROM `xf_ah_dev_tracker_entry`

WHERE (`xf_ah_dev_tracker_entry`.`entry_id` = 'blgnoqka')


LIMIT 1

Third: Your SELECT seems ok. On page 1:
SQL:
SELECT `xf_ah_dev_tracker_entry`.*
FROM `xf_ah_dev_tracker_entry`


ORDER BY `xf_ah_dev_tracker_entry`.`timestamp` DESC

LIMIT 49
And on page 2:
SQL:
SELECT `xf_ah_dev_tracker_entry`.*
FROM `xf_ah_dev_tracker_entry`


ORDER BY `xf_ah_dev_tracker_entry`.`timestamp` DESC

LIMIT 49 OFFSET 49

Thank you. How would I get rid of those?
 
Get rid of the $this->entrySaveProcess();.

I believe that is the area I helped you with. You shouldn't be getting new entries on the index page. And it's why I complained to you about not knowing what's going on in the rest of your code. Just because something works, doesn't make it correct when it's tied to the rest of the system. That's up to you to establish.

I would suggest re-thinking your entire system so it doesn't need to check every entry to see if it already exists.
 
Get rid of the $this->entrySaveProcess();.

I believe that is the area I helped you with. You shouldn't be getting new entries on the index page. And it's why I complained to you about not knowing what's going on in the rest of your code. Just because something works, doesn't make it correct when it's tied to the rest of the system. That's up to you to establish.

I would suggest re-thinking your entire system so it doesn't need to check every entry to see if it already exists.

I removed it and wrote it as a cron entry.
 
Last edited:
I would suggest re-thinking your entire system so it doesn't need to check every entry to see if it already exists.

I don't see how I could do that. If I don't check if they exist, it would keep saving duplicate entries.
 
Back
Top Bottom