Magento 2 Development 08: Filter and Sorting

0

 


We have displayed the blog list but it will show all the blogs. Even if we create a new customer s/he will be able to see the blogs from the previous customer. So let’s fix this.

Filter

If you think in terms of sql query then you can use WHERE user_id=$customerId but in magento we do not write raw sql queries. To apply conditions on the collection we have to use addFieldToFilter method. Let’s check out the code for better understanding, we have to edit the Block/BlogList.php because we are sending the data from here only,

<?php
namespace Webkul\BlogManager\Block;
use Magento\Customer\Model\SessionFactory;
class BlogList extends \Magento\Framework\View\Element\Template
{
public $blogCollection;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Webkul\BlogManager\Model\ResourceModel\Blog\CollectionFactory $blogCollection,
SessionFactory $customerSession,
array $data = []
) {
$this->blogCollection = $blogCollection;
$this->customerSession = $customerSession;
parent::__construct($context, $data);
}
public function getBlogs()
{
$customerId = $this->customerSession->create()->getCustomer()->getId();
$collection = $this->blogCollection->create();
$collection->addFieldToFilter('user_id', ['eq'=>$customerId]);
return $collection;
}
}

Here we have used the customer session to get the customer id. Now please take note of ->addFieldToFilter(‘user_id’, [‘eq’=>$customerId]); in the first param we have passed the column name. And in the second param we have passed an associative array, whose key represents the operators. eq means equal operator. Some other important operators are,
[“eq” => $equalValue]
[“neq” => $notEqualValue]
[“like” => $likeValue]
[“in” => [$inValues]]
[“nin” => [$notInValues]]
[“notnull” => $valueIsNotNull]
[“null” => $valueIsNull]
[“gt” => $greaterValue]
[“lt” => $lessValue]
[“gteq” => $greaterOrEqualValue]
[“lteq” => $lessOrEqualValue]
[“from” => $fromValue, “to” => $toValue]

If we do not provide any operator then it just use the equal operator, that means here we can use ->addFieldToFilter(‘user_id’, $customerId) also.


To combine multiple conditions with AND operator we have to use multiple addFieldToFilter methods. So for example, WHERE (`user_id` = ‘1’) AND (`status` != 0) can be achieved with following code,

$collection->addFieldToFilter('user_id', $customerId)
->addFieldToFilter('status', ['neq'=>0]);

Sorting

It will be better from usability standpoint if we show the latest blogs at the top. In sql we use ORDER BY to sort. Let’s see how can we sort in Magento, we have to edit the Block/BlogList.php

<?php
namespace Webkul\BlogManager\Block;
use Magento\Customer\Model\SessionFactory;
class BlogList extends \Magento\Framework\View\Element\Template
{
public $blogCollection;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Webkul\BlogManager\Model\ResourceModel\Blog\CollectionFactory $blogCollection,
SessionFactory $customerSession,
array $data = []
) {
$this->blogCollection = $blogCollection;
$this->customerSession = $customerSession;
parent::__construct($context, $data);
}
public function getBlogs()
{
$customerId = $this->customerSession->create()->getCustomer()->getId();
$collection = $this->blogCollection->create();
$collection->addFieldToFilter('user_id', $customerId)
->setOrder('updated_at', 'DESC');
return $collection;
}
}

To sort we use setOrder method where in first param we have pass the column name and in the second we have to mention whether we want sort in ascending or descending order. Here we are updating based on updated_at column so latest updated blogs will come at the top.

2021-02-09_13-37

Here you can see that the latest blog is coming at top. Also we are seeing only those blogs which have user_id same as the current customer id.

PS. We can view the actual SELECT sql query for the collection by printing the $collection->getSelect();

Post a Comment

0Comments
Post a Comment (0)
To Top