Magento 2: How to add multilevel AND and OR conditions to searchCriteria?SearchCriteria Logical OR and AND searchFiltering a Magento 2 Object RepositoryMagento search rest apiUse preconfigured builders (e.g. for SearchCriteria) in Magento 2Using searchCriteria with condition_type operators “from” and “to” in Magento 2.0Magento 2: how to use the collection walk iterator?Multi Conditionals (OR, AND) in addAttributeToFilterSearchCriteria Logical OR and AND searchMagento 2: How to disable caching for the header block?Magento 2: Sort multilevel category collection by both level and positionMagento 2 - Add a Custom Button to header.phtml
Is my background sufficient to start Quantum Computing
Tricky riddle from sister
Does friction always oppose motion?
Why is my 401k manager recommending me to save more?
Is it OK to say "The situation is pregnant with a crisis"?
What is the meaning of ゴト in the context of 鮎
"Best practices" for formulating MIPs
What happened to the Apollo 1 rocket?
What was the ASCII end of medium (EM) character intended to be used for?
usage of y" not just for locations?
What verb goes with "coup"?
Is it OK to throw pebbles and stones in streams, waterfalls, ponds, etc.?
What is the meaning of "it" in "as luck would have it"?
Can I deep fry food in butter instead of vegetable oil?
Aligning arrays within arrays within another array
How useful would a hydroelectric power plant be in the post-apocalypse world?
Old story where computer expert digitally animates The Lord of the Rings
Confusion in understanding the behavior of inductor in RL circuit with DC source
Using quotation marks and exclamation marks
Why did the Middle Kingdom stop building pyramid tombs?
Odd PCB Layout for Voltage Regulator
Russian equivalents of 能骗就骗 (if you can cheat, then cheat)
Why can't i use !(single pattern) in zsh even after i turn on kshglob?
Lenovo Legion PXI-E61 Media Test Failure, Check Cable. Exiting PXE ROM. Then restarts and works fine
Magento 2: How to add multilevel AND and OR conditions to searchCriteria?
SearchCriteria Logical OR and AND searchFiltering a Magento 2 Object RepositoryMagento search rest apiUse preconfigured builders (e.g. for SearchCriteria) in Magento 2Using searchCriteria with condition_type operators “from” and “to” in Magento 2.0Magento 2: how to use the collection walk iterator?Multi Conditionals (OR, AND) in addAttributeToFilterSearchCriteria Logical OR and AND searchMagento 2: How to disable caching for the header block?Magento 2: Sort multilevel category collection by both level and positionMagento 2 - Add a Custom Button to header.phtml
What i want to achieve is this -
attr1 = 'val1' AND attr2 = 'val2' AND ((attr_s1 = 'val_s1' AND attr_d1
= 'val_d1' AND attr_t1 = 'val_t1') OR (attr_s2 = 'val_s2' AND attr_d2 = 'val_d2' AND attr_t2 = 'val_t2') OR (attr_s3 = 'val_s3' AND attr_d3 = 'val_d3' AND attr_t3 = 'val_t3'))
in short
P and Q and ( (A and B and C) or (L and M and N) or (X and Y and Z) )
I have gone through similar kind of thread here -
SearchCriteria Logical OR and AND search
But what I want is much more complex.
Can it be done with searchCriteria or in any other way?
magento2 collection search-criteria
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
What i want to achieve is this -
attr1 = 'val1' AND attr2 = 'val2' AND ((attr_s1 = 'val_s1' AND attr_d1
= 'val_d1' AND attr_t1 = 'val_t1') OR (attr_s2 = 'val_s2' AND attr_d2 = 'val_d2' AND attr_t2 = 'val_t2') OR (attr_s3 = 'val_s3' AND attr_d3 = 'val_d3' AND attr_t3 = 'val_t3'))
in short
P and Q and ( (A and B and C) or (L and M and N) or (X and Y and Z) )
I have gone through similar kind of thread here -
SearchCriteria Logical OR and AND search
But what I want is much more complex.
Can it be done with searchCriteria or in any other way?
magento2 collection search-criteria
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
What i want to achieve is this -
attr1 = 'val1' AND attr2 = 'val2' AND ((attr_s1 = 'val_s1' AND attr_d1
= 'val_d1' AND attr_t1 = 'val_t1') OR (attr_s2 = 'val_s2' AND attr_d2 = 'val_d2' AND attr_t2 = 'val_t2') OR (attr_s3 = 'val_s3' AND attr_d3 = 'val_d3' AND attr_t3 = 'val_t3'))
in short
P and Q and ( (A and B and C) or (L and M and N) or (X and Y and Z) )
I have gone through similar kind of thread here -
SearchCriteria Logical OR and AND search
But what I want is much more complex.
Can it be done with searchCriteria or in any other way?
magento2 collection search-criteria
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
What i want to achieve is this -
attr1 = 'val1' AND attr2 = 'val2' AND ((attr_s1 = 'val_s1' AND attr_d1
= 'val_d1' AND attr_t1 = 'val_t1') OR (attr_s2 = 'val_s2' AND attr_d2 = 'val_d2' AND attr_t2 = 'val_t2') OR (attr_s3 = 'val_s3' AND attr_d3 = 'val_d3' AND attr_t3 = 'val_t3'))
in short
P and Q and ( (A and B and C) or (L and M and N) or (X and Y and Z) )
I have gone through similar kind of thread here -
SearchCriteria Logical OR and AND search
But what I want is much more complex.
Can it be done with searchCriteria or in any other way?
magento2 collection search-criteria
magento2 collection search-criteria
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Jun 24 at 8:27
Amol AgreAmol Agre
62 bronze badges
62 bronze badges
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Amol Agre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Try following way:
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCmsModelBlockRepository;
DI
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var BlockRepository
*/
private $blockRepository;
public function __construct(
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
BlockRepository $blockRepository
)
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
Now:
$filter = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('men-block')
->create();
$filter2 = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('gear-block')
->create();
$filterGroup = $this->filterGroupBuilder->setFilters([$filter, $filter2])
->create();
$filter3 = $this->filterBuilder->setField('is_active')
->setConditionType('eq')
->setValue(1)
->create();
$filterGroup2 = $this->filterGroupBuilder->setFilters([$filter3])
->create();
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterGroup, $filterGroup2])
->create();
$blockItems = $this->blockRepository->getList($searchCriteria)->getItems();
Query:
SELECT `main_table`.* FROM `cms_block` AS `main_table` WHERE ((`identifier` = 'men-block') OR (`identifier` = 'gear-block')) AND ((`is_active` = 1))
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Amol Agre is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279356%2fmagento-2-how-to-add-multilevel-and-and-or-conditions-to-searchcriteria%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try following way:
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCmsModelBlockRepository;
DI
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var BlockRepository
*/
private $blockRepository;
public function __construct(
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
BlockRepository $blockRepository
)
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
Now:
$filter = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('men-block')
->create();
$filter2 = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('gear-block')
->create();
$filterGroup = $this->filterGroupBuilder->setFilters([$filter, $filter2])
->create();
$filter3 = $this->filterBuilder->setField('is_active')
->setConditionType('eq')
->setValue(1)
->create();
$filterGroup2 = $this->filterGroupBuilder->setFilters([$filter3])
->create();
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterGroup, $filterGroup2])
->create();
$blockItems = $this->blockRepository->getList($searchCriteria)->getItems();
Query:
SELECT `main_table`.* FROM `cms_block` AS `main_table` WHERE ((`identifier` = 'men-block') OR (`identifier` = 'gear-block')) AND ((`is_active` = 1))
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
add a comment |
Try following way:
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCmsModelBlockRepository;
DI
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var BlockRepository
*/
private $blockRepository;
public function __construct(
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
BlockRepository $blockRepository
)
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
Now:
$filter = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('men-block')
->create();
$filter2 = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('gear-block')
->create();
$filterGroup = $this->filterGroupBuilder->setFilters([$filter, $filter2])
->create();
$filter3 = $this->filterBuilder->setField('is_active')
->setConditionType('eq')
->setValue(1)
->create();
$filterGroup2 = $this->filterGroupBuilder->setFilters([$filter3])
->create();
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterGroup, $filterGroup2])
->create();
$blockItems = $this->blockRepository->getList($searchCriteria)->getItems();
Query:
SELECT `main_table`.* FROM `cms_block` AS `main_table` WHERE ((`identifier` = 'men-block') OR (`identifier` = 'gear-block')) AND ((`is_active` = 1))
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
add a comment |
Try following way:
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCmsModelBlockRepository;
DI
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var BlockRepository
*/
private $blockRepository;
public function __construct(
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
BlockRepository $blockRepository
)
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
Now:
$filter = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('men-block')
->create();
$filter2 = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('gear-block')
->create();
$filterGroup = $this->filterGroupBuilder->setFilters([$filter, $filter2])
->create();
$filter3 = $this->filterBuilder->setField('is_active')
->setConditionType('eq')
->setValue(1)
->create();
$filterGroup2 = $this->filterGroupBuilder->setFilters([$filter3])
->create();
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterGroup, $filterGroup2])
->create();
$blockItems = $this->blockRepository->getList($searchCriteria)->getItems();
Query:
SELECT `main_table`.* FROM `cms_block` AS `main_table` WHERE ((`identifier` = 'men-block') OR (`identifier` = 'gear-block')) AND ((`is_active` = 1))
Try following way:
use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCmsModelBlockRepository;
DI
/**
* @var FilterBuilder
*/
private $filterBuilder;
/**
* @var FilterGroupBuilder
*/
private $filterGroupBuilder;
/**
* @var SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;
/**
* @var BlockRepository
*/
private $blockRepository;
public function __construct(
FilterBuilder $filterBuilder,
FilterGroupBuilder $filterGroupBuilder,
SearchCriteriaBuilder $searchCriteriaBuilder,
BlockRepository $blockRepository
)
$this->filterBuilder = $filterBuilder;
$this->filterGroupBuilder = $filterGroupBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->blockRepository = $blockRepository;
Now:
$filter = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('men-block')
->create();
$filter2 = $this->filterBuilder->setField('identifier')
->setConditionType('eq')
->setValue('gear-block')
->create();
$filterGroup = $this->filterGroupBuilder->setFilters([$filter, $filter2])
->create();
$filter3 = $this->filterBuilder->setField('is_active')
->setConditionType('eq')
->setValue(1)
->create();
$filterGroup2 = $this->filterGroupBuilder->setFilters([$filter3])
->create();
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterGroup, $filterGroup2])
->create();
$blockItems = $this->blockRepository->getList($searchCriteria)->getItems();
Query:
SELECT `main_table`.* FROM `cms_block` AS `main_table` WHERE ((`identifier` = 'men-block') OR (`identifier` = 'gear-block')) AND ((`is_active` = 1))
answered Jun 24 at 9:39
Sohel RanaSohel Rana
24.6k3 gold badges47 silver badges65 bronze badges
24.6k3 gold badges47 silver badges65 bronze badges
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
add a comment |
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
This is like - (A or B) and C.. not answering my scenario
– Amol Agre
Jun 24 at 10:51
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
I posted one example how you create and/or single query. You can create this what you want from here
– Sohel Rana
Jun 24 at 11:37
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
Yes. I read it in documentation. But it allows only AND and OR on 1 level. Filtergroups AND, Filters OR.. I need 1 more level
– Amol Agre
Jun 24 at 11:42
add a comment |
Amol Agre is a new contributor. Be nice, and check out our Code of Conduct.
Amol Agre is a new contributor. Be nice, and check out our Code of Conduct.
Amol Agre is a new contributor. Be nice, and check out our Code of Conduct.
Amol Agre is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f279356%2fmagento-2-how-to-add-multilevel-and-and-or-conditions-to-searchcriteria%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown