Remove specific criteria from Varien_Db_Select join
-
28-09-2020 - |
Question
I have Varien_Db_Select
object with some joins and I need to change condition in one of them. How can I do this?
update: I found that condition in "from" part and then in "joinCondition" part. I can loop through, but I can't put them back
Solution
So this is what I came up with:
$fromAndJoins = $select->getPart(Zend_Db_Select::FROM);
foreach($fromAndJoins as $key=>$joins){
if(strpos($joins['joinCondition'],$attribute->getAttributeCode().'_idx.attribute_id')!==false){
$newcondition = //change $joins['joinCondition'] as you want
$fromAndJoins[$key]['joinCondition']=$newcondition;
}
}
$select->reset(Zend_Db_Select::FROM);
$select->setPart(Zend_Db_Select::FROM,$fromAndJoins);
OTHER TIPS
Here is the method I would use.
You can get the conditions ("where" part) like this:
$where = $collection->getSelect()->getPart('where');
Then you can loop through the conditions like this:
foreach ($where as $key => $condition)
{
// Do what you need
}
Finally once you've made the changes you needed, don't forget to apply the updated condition to your colllection:
$collection->getSelect()->setPart('where', $where);
If you get the error
You cannot define a correlation name 'xxx' more than once
you have to unset($fromAndJoins[$key]);
item, where the key was in my case
$key = $attribute->getAttributeCode().'_idx.attribute_id';
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange