I need to known how to use addFieldToFilter & addFieldToSelect in frontend.? And can i use like below,

            $searchQuery = $wholeData["searchQuery"] ?? "";
            $searchQuery = trim($searchQuery);
            $agentCollection = $this->_agents->create()->addNameToSelect();

            if ($agentId > 0) {
                $agentCollection->addFieldToFilter("id", $agentId);
            } else {
                            ["field"=>"email", "like"=>"%".$searchQuery."%"],
                            ["field"=>"contactno", "like"=>"%".$searchQuery."%"]

1) yes you can use this logic in front end but in block classes. 2) yes above logic can be used but it will add OR logic in you case same as following in better way

        ['weight', 'name'],
        [['in' => [1, 3]], ['like' => 'M%']]

Can be used as AND logic:

$category = $this->collectionFactory->create()
            ->addFieldToFilter('parent_id', $rootCategoryId)
            ->addFieldToFilter('name', $categoryName)
  • what is this addFieldToFilter – jibin george Nov 12 at 16:58
  • addFieldToFilter is similar to sql part : where name = 'hassan' – Hassan Ali Shahzad Nov 12 at 19:37
  • SELECT time FROM list WHERE date = "2020-10-12" GROUP BY time; HOw would you do this? – jibin george Nov 13 at 9:00
  • $collection = $this->collectionFactory->create(); $collection->addAttributeToSelect('*'); $collection->addAttributeToFilter('date', $date); $collection->getSelect()->reset(\Zend_Db_Select::COLUMNS) ->columns('time'); $collection->getSelect()->group('time'); git it from here magento.stackexchange.com/questions/295358/… – jibin george Nov 13 at 9:01
  • what are you doing? in above you are collecting all attributes by using * and the reset again all element and select 'time'. why don't you put $collection->addAttributeToSelect('time'); ? – Hassan Ali Shahzad Nov 13 at 9:10

Followings are my findings about your question:

  1. What does "use addFieldToFilter & addFieldToSelect in frontend." means?
  2. Yes, you can use addFieldToFilter as you define.
    • If email column is in your table, then you don't need to add email in to select.
    • Use Add field to select when you are using this with the select column in another table when joining.

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.