Magento2 Filter and Sorting

我们已经显示了博客列表,但是它将显示所有博客。 即使我们创建了新客户,他/她也将能够看到前一位客户的博客。 因此,让我们解决此问题。

Filter

如果您认为是根据sql查询,则可以使用WHERE user_id = $ customerId,但在magento中,我们不编写原始sql查询。 要对集合应用条件,我们必须使用addFieldToFilter方法。 让我们检查一下代码以更好地理解它,我们必须编辑Block / BlogList.php,因为我们仅从此处发送数据,

<?php

namespace Yshuq\BlogManager\Block;

use Magento\Customer\Model\Session;

class BlogList extends \Magento\Framework\View\Element\Template
{
    public $blogCollection;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Yshuq\BlogManager\Model\ResourceModel\Blog\CollectionFactory $blogCollection,
         Session $customerSession,
        array $data = []
    ) {
        $this->blogCollection = $blogCollection;
        $this->customerSession = $customerSession;
        parent::__construct($context, $data);
        parent::__construct($context, $data);
    }

    public function getBlogs()
    {
        $customerId = $this->customerSession->getCustomer()->getId();

        $collection = $this->blogCollection->create();
        $collection->addFieldToFilter('user_id', ['eq'=>$customerId]);
        return $collection;
    }
}

在这里,我们使用了客户会话来获取客户ID。 现在,请注意-> addFieldToFilter(’user_id’,[‘eq’=> $ customerId]); 在第一个参数中,我们已经传递了列名。 在第二个参数中,我们传递了一个关联数组,其键代表运算符。 eq表示相等的运算符。 其他一些重要的运算符是

[“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]

如果我们不提供任何运算符,则仅使用等于运算符,这意味着在这里我们也可以使用-> addFieldToFilter(’user_id’,$ customerId)。

要将多个条件与AND运算符结合使用,我们必须使用多个addFieldToFilter方法。 因此,例如,可以使用以下代码实现WHERE(user_id =’1’)AND(status!= 0),

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

Sorting

从可用性的角度来看,如果我们在顶部显示最新的博客会更好。 在sql中,我们使用ORDER BY进行排序。 让我们看看如何在Magento中进行排序,我们必须编辑Block / BlogList.php

<?php

namespace Yshuq\BlogManager\Block;

use Magento\Customer\Model\Session;

class BlogList extends \Magento\Framework\View\Element\Template
{
    public $blogCollection;
    /**
     * @var Session
     */
    private $customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Yshuq\BlogManager\Model\ResourceModel\Blog\CollectionFactory $blogCollection,
         Session $customerSession,
        array $data = []
    ) {
        $this->blogCollection = $blogCollection;
        $this->customerSession = $customerSession;
        parent::__construct($context, $data);
    }

    public function getBlogs()
    {
        $customerId = $this->customerSession->getCustomer()->getId();
        $collection = $this->blogCollection->create();
        $collection->addFieldToFilter('user_id', ['eq'=>$customerId])
            ->setOrder('updated_at', 'DESC');
        return $collection;
    }
}

要进行排序,请使用setOrder方法,其中在第一个参数中,我们传递了列名,在第二个参数中,我们必须提及我们要按升序还是降序排序。 在这里,我们根据updated_at列进行更新,因此最新的更新博客将位于顶部。

列表展示

在这里,您可以看到最新的博客排在最前面。 另外,我们仅看到那些user_id与当前客户ID相同的博客。

注意。 我们可以通过打印$ collection-> getSelect();查看集合的实际SELECT sql查询。

代码如下:

public function getBlogs()
    {
        $customerId = $this->customerSession->getCustomer()->getId();
        $collection = $this->blogCollection->create();
        echo $collection->getSelect()."<br>";
        $collection->addFieldToFilter('user_id', ['eq'=>$customerId])
            ->setOrder('updated_at', 'DESC');
        echo $collection->getSelect();
        return $collection;
    }

输出sql结果如下:

输出sql语句

我们尚未创建任何文件,因此文件夹结构将保持不变。