How to select discriminator column in doctrine 2

北慕城南 提交于 2019-12-01 17:52:19
Matteo

There is no direct access to the discriminator column.

It may happen that the entities of a special type should be queried. Because there is no direct access to the discriminator column, Doctrine provides the INSTANCE OF construct.

You can query for the type of your entity using the INSTANCE OF DQL as described in the docs. As example:

$query = $em->createQuery("SELECT product FROM AppBundle\Entity\AbstractProduct product WHERE product  INSTANCE OF AppBundle\Entity\Product");
$products = $query->getResult();

Hope this helps

I use this little "hack"

  1. Define a common interface for your entities (optional but recommended)
  2. Create a getType method in this interface
  3. Create constant into Discriminator entity
  4. Return the proper constant inside every discriminated entity

That way you can retrieve the discriminator "generic" entity (Product in your case) and call getType onto it.

Of course if you're interested into result filtering done directly by sql, this is not a solution at all and, I'm afraid, there isn't any solution available at the moment.
If you find one better that this, please share with us.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!