2

I would like to get Cms Pages from repository using search criteria. I need to filter title and content fields. This is how I'm trying to do this:

$this->filterBuilder->setField('title');
$this->filterBuilder->setValue('%keyword%');
$this->filterBuilder->setConditionType('like');
$filters[] = $this->filterBuilder->create();

$this->filterBuilder->setField('content');
$this->filterBuilder->setValue('%keyword%');
$this->filterBuilder->setConditionType('like');
$filters[] = $this->filterBuilder->create();

$this->searchCriteriaBuilder->addFilters($filters);

and it gives me following SQL:

SELECT `main_table`.* FROM `cms_page` AS `main_table` WHERE (`title` = '%keyword%') AND (`content` = '%keyword%')

while what I need is:

SELECT `main_table`.* FROM `cms_page` AS `main_table` WHERE (`title` = '%keyword%') OR (`content` = '%keyword%')
1
  • How have print this sql query? I am trying by : echo (string) $this->searchCriteriaBuilder->addFilter($filter)->getSelect(); But getting error: "Uncaught Error: Call to undefined method Magento\Framework\Api\Search\SearchCriteriaBuilder::getSelect()"
    – D Singh
    Jul 9 '20 at 12:53
3

You can try with,

$title = $this->filterBuilder->setField('title')
            ->setValue('%keyword%')
            ->setConditionType('like')
            ->create();

$content = $this->filterBuilder->setField('content')
            ->setValue('%keyword%')
            ->setConditionType('like')
            ->create();

$filterOr = $this->filterGroupBuilder
        ->addFilter($title)
        ->addFilter($content)
        ->create();

$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterOr])->create();

$this->searchCriteriaBuilder->addFilters($searchCriteria);
1
  • you can try comment out this line, //$this->searchCriteriaBuilder->addFilters($searchCriteria); and print $searchCriteria only. Nov 17 '16 at 12:49

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.