教义:如何从侦听器(preDqlSelect)内的选择查询中删除部分 where 子句?

2022-01-16 00:00:00 php doctrine listener behavior

我的侦听器是行为的一部分,它应该删除任何调用的选择查询的 where 子句中的所有 is_published 检查.将部分添加到子句非常容易,但是如何删除.

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.

有一些函数如Doctrine_Query->removeDqlQueryPart('where'),但这删除了完整的 where 子句,而我只需要删除 'is_published = ?' 部分.

There are some functions like Doctrine_Query->removeDqlQueryPart('where'), but that removes the complete where clause, while I only need the 'is_published = ?' part to be removed.

但是,我可以使用正则表达式或其他方式手动处理此问题.但棘手的部分是,如何删除?"表示的参数.从相应的参数数组(可通过 Doctrine_Query->getRawParams() 检索).

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 Doctrine_Query->getRawParams()).

所以我问,有没有一种干净的方法来转换这种查询:
...FROM Video v WHERE v.is_published = ?AND v.start_date <?AND v.end_date >?

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 > ?

到此剥离一个并且不会弄乱问号表示的参数:
...FROM Video v WHERE v.start_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 > ?

这当然只是一个简单的例子,我的查询要复杂一些.不幸的是,由于 symfony 框架,我坚持使用 1.0.x 学说.

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.

推荐答案

调用 $query->getDqlPart('where') 将返回一个 array 部分通过 where()andWhere() 等函数添加 where 子句.因此,您可以使用它来查找和删除您想要的部分.

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.

然后你必须处理参数.骑自行车时,您需要找到所有零件吗?并计算它们并记住您删除的任何数字的数字,然后调用 $params = $query->getParams(); 并且 where 子句参数将在 $params['where'] 这样您就可以从那里删除它们,然后调用 $query->setParams($params);

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

相关文章