4

This is my block file, everything is working fine except setPageSize() and setCurPage() functions at last. Regardless of these two filters it is allowing all the rows to be fetched. What could be the reason?

public function __construct(
    //.....
    \Magento\Framework\Data\CollectionFactory $collectionFactory
) {
    //.....
    $this->collectionFactory = $collectionFactory;

}

public function getListProduct()
{
    $collection = $this->collectionFactory->create();

    $fromDate = date('Y-m-d H:i:s', strtotime("-6 month"));
    $product = $this->customCategoryFactory->create()
        ->addFieldToFilter('user_id', ['eq' => $loggedInUser])
        ->addFieldToFilter('logged_at', ['from' => $fromDate])
        ->addFieldToSelect('cat_id')
        ->distinct(true);

    $product1 = $this->anotherCustomCategoryFactory->create()
        ->addFieldToFilter('user_id', ['eq' => $loggedInUser])
        ->addFieldToFilter('logged_at', ['from' => $fromDate])
        ->addFieldToSelect('cat_id')
        ->distinct(true);
    //I'm getting the correct collection of products.

    foreach ($product as $key => $value) {
        $varienObject = new \Magento\Framework\DataObject();
        $varienObject->setData($value->getData());
        $collection->addItem($varienObject);
    }
    foreach ($product1 as $key => $value) {
        $varienObject = new \Magento\Framework\DataObject();
        $varienObject->setData($value->getData());
        $collection->addItem($varienObject);
    }
    //now I need to use this collection for pagination.

    $page = ($this->getRequest()->getParam('p')) ? $this->getRequest()->getParam('p') : 1;
    $pageSize = ($this->getRequest()->getParam('limit')) ? $this->getRequest()->getParam('limit') : 1;

    $collection->setPageSize($pageSize);
    $collection->setCurPage($page);
}
5

The main reason why the issue occurs is that the methods setPageSize and setCurPage are not applied in the load method. This method is called from the loadData one, that in turn, must be called in case when the collection is not uploaded and when its items are called (the method getItems for example).

This confusion usually occurs due to the fact that the loadData method is different in its class from the majority of the regular collections - Magento\Framework\Data\Collection\AbstractDb.

When loading, it activates the call:

$this->_renderFilters()->_renderOrders()->_renderLimit();

At the same time, when loading in the indicated class collection, it returns the current class instance. You can check that on your own, when you have a look at the code of the method: loadData in the class Magento\Framework\Data\Collection:

/**
 * Load data
 *
 * @param bool $printQuery
 * @param bool $logQuery
 * @return $this
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function loadData($printQuery = false, $logQuery = false)
{
    return $this;
}

Hence, for for the current class instance, these filters have no effect. Nevertheless, they can be added, as it's implied by the interface and any class that activates it, should support it as well.

For your personal purposes, I'd recommend you to extend the class of the collection with your own class and implement there the missing logic. You may need that in the future. :)

  • I'm in beginning phase of learning magento, whatever you explained seems promising. I tried to figure out if I could get a solution to my task. But I could not. You understood what I intend to do. Could you please show me in code before I accept this as right answer? – Purushotam Sangroula Jun 5 '17 at 15:39
  • what sort of missing logic could I implement to make it work? – Purushotam Sangroula Jun 6 '17 at 3:13
0

Though I could not implement the solution suggested by @MageWorx but I had to retrieve single collection by using product ids of two collection. I made it work this way which could not be the desired solution. But works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.