virtualize virtualize - 1 year ago 85
PHP Question

Doctrine: How to remove part of a where clause from select query inside listener (preDqlSelect)?

My listener is part of a behavior, that should remove all is_published checks in the where clause of any called select query. Adding a part to a clause is really easy, but how to remove one.

There are some functions like

but that removes the complete where clause, while I only need the
'is_published = ?'
part to be removed.

However I could handle this manually somehow, with regex or something. But the tricky part is, how to remove the parameter represented by the '?' from the corresponding parameters array (retrievable by

So I ask, is there a clean way to transform this kind of query:

...FROM Video v WHERE v.is_published = ? AND v.start_date < ? AND v.end_date > ?

to this stripped one and without messing up the params represented by the question marks:

...FROM Video v WHERE v.start_date < ? AND v.end_date > ?

This is of course just a simple example, my queries are a bit more complex.
Unfortunately I'm stuck with doctrine 1.0.x because of the symfony framework.

Answer Source

Calling $query->getDqlPart('where') will return an array of the parts of the where clause as they were added via the where(), andWhere(), etc functions. So you can use that to find and remove the part you want.

Then you have to deal with the params. While cycling through the where parts you would need to find all ? and count them and remember the numbers for any of the ones you remove and then call $params = $query->getParams(); and the where clause parameters will be in $params['where'] so you can remove them from there and then call $query->setParams($params);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download