4

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

7

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);
3

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);
1

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';

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.