在Simfony2实体字段类型中从原则查询到QueryBuilder [英] From Doctrine Query to QueryBuilder in a Simfony2 entity field type

查看:97
本文介绍了在Simfony2实体字段类型中从原则查询到QueryBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Symfony2.1 表单中的实体字段类型。在这里,我将使用 query_builder param只返回匹配长复杂查询的实体(请参见官方文档)。

I'm using the entity Field Type in a Symfony2.1 form. Here, I'm going to use the query_builder param to return only entities that matches a long-complex query (see the example in the official docs).

显然,实体字段类型的 query_builder 参数接受了一个Doctrine QueryBuilder 对象。另一方面,我有大型实体存储库,其中包含由EntityManager 的 createQuery()函数获取的复杂DQL查询,该函数返回一个Doctrine 查询对象。
所以,我不能直接在实体字段类型中使用所有这些查询。此外,重写用QueryBuilder使用的所有查询将是无意义的。

Obviously the query_builder param for the entity field type accepts a Doctrine QueryBuilder object. On the other side, I have large entity repositories with complex DQL queries obtained by the EntityManager's createQuery() function which returns a Doctrine Query object. So, I cannot directly use all these queries in the entity field type. Moreover, rewriting all the queries for the use with a QueryBuilder would be a non-sense.

有没有这样一种方式可以自动从查询 object to the QueryBuilder object?

Is there such a way to automatically translate from the Query object to the QueryBuilder object?

推荐答案

从Symfony2 docs:

From Symfony2 docs:


query_builder - 键入: Doctrine\ORM\QueryBuilder Closure <...

query_builder - type: Doctrine\ORM\QueryBuilder or a Closure <---

如果指定,这用于查询选项子集(和他们的
订单)应该用于该字段。该选项的值可以是
或者是$ code> QueryBuilder 对象或 Closure 。如果使用 Closure ,它
应该采用一个参数,即$ b $的$ code> EntityRepository $ b实体。

If specified, this is used to query the subset of options (and their order) that should be used for the field. The value of this option can either be a QueryBuilder object or a Closure. If using a Closure, it should take a single argument, which is the EntityRepository of the entity.

现在,我没有时间尝试一个例子,但在我看来,如果你使用关闭您可以返回目标的 ArrayCollection (或至少数组)实体对象。您的 Closure 获取 EntityRepository 的对象作为参数,因此无需重写所有内容。

Now, I haven't got time to try an example but it seems to me that if you use Closure you could return ArrayCollection (or at least array) of target entity objects. Your Closure gets object of EntityRepository as an argument so there's no need to rewrite all that stuff.

注意给它一个镜头? :)

Mind giving it a shot? :)

...抱歉让...等待...

... sorry for kept you waiting...

似乎这是不可能的。相反,您必须手动使用选择表单类型和Feed实体对象(或对象存储库)。

It seems that it's not possible this way. Instead you would have to use choice form type and feed entity objects (or objects repository as I did) manually.

我在这里做了一些简化的例子: http://ideone.com/LHdi2E

I've made some simplified example here: http://ideone.com/LHdi2E

希望这有帮助...

这篇关于在Simfony2实体字段类型中从原则查询到QueryBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆