Doctrine2:多态查询:搜索子类的属性 [英] Doctrine2: Polymorphic Queries: Searching on properties of subclasses

查看:96
本文介绍了Doctrine2:多态查询:搜索子类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理客户订单的项目。其中一些订单是通过Amazon.com进行的。所以我有一个Order实体和一个扩展它的AmazonOrder实体。 AmazonOrder添加的一件事是AmazonOrderId。



我需要实现广泛的搜索功能。用户可以在文本框中输入一些东西,并在一个big where-clause中用于一堆表达式。因此,例如,如果用户搜索111,结果包括以111开头的任何订单,任何订单都以111开头的邮政编码发货,任何订单运到111 Main St等



这些东西是用查询构建器创建的查询实现的,该查询具有大的 orX()表达式。



现在,我想匹配所有订单,但如果他们是AmazonOrder,也匹配AmazonOrderId。



我被卡住了 - 我怀疑可能不可能



以下是我建立查询的方式:

$ b从('PMS\Entity\Order','o')选择('o,s') - >
$ b

  $ qb-> ; 
$ qb-> leftJoin('o.shippingInfo','s');
$ qb-> andWhere('o.status =:status');
$ qb-> setParameter('status',$ status);
$ qb-> andWhere(
$ qb-> expr() - > orX(
$ qb-> expr() - > like('o.id' ,':query')
$ qb-> expr() - > like('s.address',':query')
$ qb-> expr() - > ('s.city',':query')

);
$ qb-> setParameter('query',$ userQuery。'%');

$ orders = $ qb-> getQuery() - > getResult();

我不知道如何添加一个条件,粗略地说OR(Order是一个AmazonOrder和AmazonOrderId LIKE'$ userQuery%')



任何人都有洞察力?或者一个方法来处理这个问题,还是至少确认这样做不行?

解决方案

嗯,我有类似的问题在我最后的学说项目。



有一次它只是一个字段,所以我把它移动到父类 - 而不是最好的解决方案,但工作。在某些其他情况下,这些属性太多,这样会混淆父类。我做了一个本机sql查询,用于搜索和获取记录ids,然后使用 WHERE IN(...) dql以获取实体。



妥协可能是教条 ResultSetMapping ,可以直接将本机SQL查询映射到实体,虽然每次使用它时,我发现使用起来很笨拙,如上所述,两个查询(提取ids和获取元数据)的开销是可忽略的。



也许您可以使用 INSTANCEOF 操作符在你的 WHERE 子句中,虽然我不认为教条会聪明地认识到你想要的方式。


I've got a project where I deal with customer orders. Some of those orders are made via Amazon.com. So I've got an Order entity, and an AmazonOrder entity that extends it. One thing added by AmazonOrder is the AmazonOrderId.

I've a requirement to implement a broad search feature. The user can enter some stuff into a text box, and be used in a bunch of expressions in one big where-clause. So, for example, if the user searched for "111", the results include any orders with an ID starting with 111, any order being shipped to zip codes that begin with 111, any order being shipped to "111 Main St", etc.

That stuff is implemented with a query-builder-created query that has a big orX() expression.

Now, I'd like to match against all Orders, but if they're an AmazonOrder, also match against AmazonOrderId.

And I'm stuck -- I suspect it may not be possible

Here's how I'm building up the query:

$qb->select('o,s')->from('PMS\Entity\Order', 'o');    
$qb->leftJoin('o.shippingInfo','s');
$qb->andWhere('o.status = :status');
$qb->setParameter('status',$status);
$qb->andWhere(
    $qb->expr()->orX(
        $qb->expr()->like('o.id',':query')
        $qb->expr()->like('s.address',':query')
        $qb->expr()->like('s.city',':query')
    )
);
$qb->setParameter('query',$userQuery .'%');

$orders = $qb->getQuery()->getResult();

And I can't figure out how to add a condition that says, roughly, "OR (Order is an AmazonOrder AND AmazonOrderId LIKE '$userQuery%')"

Anyone have any insight? Either a way to handle this, or at least a confirmation that it's not doable this way?

解决方案

Hm, I had similiar problems in my last doctrine project.

One time it was just a single field, so I moved it to the parent class – not the nicest solution, but worked. In some other case there where too many properties so these would have cluttered the parent class. I did a native sql query for searching and fetching me the record ids and then used a WHERE IN (...) dql in order to fetch the entities.

A compromise might be the doctrine ResultSetMapping which can map a native sql query to entities directly, although every time I worked with it I found it quite clumsy to use and the overhead for two queries (fetch ids & fetch entites) as outlined above to be neglectable.

Maybe you could accomplish something with the INSTANCEOF operator in your WHERE clause, although I dont think doctrine would be smart enough to recognize it the way you want.

这篇关于Doctrine2:多态查询:搜索子类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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