문제

I have a custom collection to which I add filters. For example, I have this:

$giftColletion = $this->_giftFactory->getCollection();
$giftColletion->addFieldToFilter('store_id', 1);

What filter should I add to $giftColletion so it can return all the records by field position in ASC order ?

도움이 되었습니까?

해결책

You can try this

$giftColletion = $this->_giftFactory->getCollection();
$giftColletion->addFieldToFilter('store_id', 1);
$giftColletion->setOrder('position','ASC');

setOrder is use for sorting

다른 팁

According to \Magento\Catalog\Model\ResourceModel\Product\Collection you can use addAttributeToSort() method to sort your collection.

This has worked for me:

$collection = $this->_collection
    ->create()
    ->addAttributeToSelect(['sku', 'name', 'image'])
    ->addCategoryFilter($category)
    ->addAttributeToSort('name')
    ->setPageSize($limit);
return $collection;

Use whatever attribute you need your collection to be sorted by instead of 'name' in my example. You can also specify order direction as the second parameter, it defaults to ASC.

Try this,

$page = ($this->getRequest()->getParam('p')) ? $this->getRequest()->getParam('p') : 1;
$pageSize = 10;
$allMagazines = $this->MagazineFactory->create()->getCollection();
$allMagazines->addFieldToFilter('is_enable', 1);
$allMagazines->setOrder('sort_order','ASC');
$allMagazines->setPageSize($pageSize);
$allMagazines->setCurPage($page);
return $allMagazines;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top