本文介绍了教义:如何从侦听器(preDqlSelect)内的选择查询中删除部分 where 子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的侦听器是行为的一部分,它应该删除任何调用的选择查询的 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 hDoctrine_Query->getRawParams()).

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

So I ask, is there a clean way to transform this kind of query:
...FROM Video v WHERE v.is_published = ?

到此剥离一个并且不会弄乱问号表示的参数:
...FROM Video v WHERE v.start_date

to this stripped one...FROM Video v WHERE v.start_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() 等函数添加 where 子句.因此,您可以使用它来查找和删除您想要的部分.

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

然后你必须处理参数.骑自行车时,您需要找到所有零件吗?并计算它们并记住您删除的任何数字的数字,然后调用 $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 ? $params = $query->getParams(); $params['where'] so you can remove them from there $query->setParams($params);

这篇关于教义:如何从侦听器(preDqlSelect)内的选择查询中删除部分 where 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 09:25
查看更多