问题描述
我的监听器是行为的一部分,应该删除任何被调用的select查询的where子句中的所有is_published检查。添加一个子句是很容易的,但是如何去除一个。
有些功能像 Doctrine_Query-> removeDqlQueryPart('where')
,
删除完整的where子句,而我只需要删除'is_published =?'
部分。
然而,我可以用正则表达式或某些东西来手动处理这个。但是棘手的部分是,如何从相应的参数数组中删除由'?'表示的参数(可由 Doctrine_Query-> getRawParams()
检索)
所以我问,是否有一种干净的方式来转换这种查询:
... FROM Video v WHERE v。 is_published =? AND v.start_date< ? AND v.end_date> ??/ code
$ p
$ c> ... FROM Video v WHERE v.start_date< ? AND v.end_date> ?
这当然只是一个简单的例子,我的查询有点复杂。
不幸的是,由于symfony框架,我坚持使用doctrine 1.0.x。
调用 $ query-> getDqlPart('where')
将返回where子句部分的数组
,因为它们是通过 where()
, andWhere()
等功能。所以你可以使用它来查找和删除你想要的部分。
然后你必须处理参数。在骑自行车的地方,你需要找到所有的部分?并计数它们并记住您删除的任何数字,然后调用$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ params ['where']
所以你可以从那里删除它们,然后调用$ $ $ $ $ $ $ $ $ $ set code>
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 Doctrine_Query->removeDqlQueryPart('where')
,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 Doctrine_Query->getRawParams()
).
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.
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);
这篇关于Doctrine:如何从listener(preDqlSelect)中的select query中删除where子句的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!