Magento Collection - Multi-Field Filter

Using Magentos collection models, how should I add a query part / filter, for example:

WHERE (main_table.x < 1 OR (main_table.x - main_table.y) >= 5) 

Update I now run this:

 $this->getSelect() ->where('main_table.x < 1') ->orWhere('(main_table.x - main_table.y) >= :qty'); $this->addBindParam(':qty', $qty); 

Result:

 SELECT ... WHERE ... AND ... AND (main_table.x < 1) OR ((main_table.x - main_table.y) >= :qty) ORDER BY ... 

The problem is that I am unable to bind $qty to :qty

Update 2 I ended up with this since I needed an OR in parentheses

 $this->getSelect()->where('(main_table.x < 1 OR (main_table.x - main_table.y) >= ?)', $qty); 
+7
source share
2 answers

When you use the getSelect method, you getSelect Magento model collection interface. Sometimes this is the only way to get the exact selection request you want, but keep in mind that it cannot rot 100% with what the Magento model interface does.

When you use the bindParamater method, you are using the Magento model interface. I canโ€™t talk with why it doesnโ€™t work, but I suspect that the Zend picker and the Magento model collection objects bind their parameters at different times and differently.

To get the desired results, skip the bindParamater method and use a simpler parameter ? to replace the orWhere method.

 $this->getSelect() ->where('main_table.x < 1') ->orWhere('(main_table.x - main_table.y) >= ?',$qty); 
+6
source

Blockquote The problem is that I cannot associate $ qty with: qty

Well, this is actually not a problem, since the PDO / MySQL mechanism works with the parameters for preparing and binding queries that are sent separately, and then execute the query.

So not on the DB abstraction layer to generate the final query request if you use the Bind parameters

See this stack question and and the PDO Guide .

+3
source

All Articles