[英]Symfony2 + Doctrine, find in relations
I'm thinking over to design an API to filter objects using fos_rest bundle in Symfony2 and Doctrine with MySQL. 我正在考虑设计一个API来使用Symfony2中的fos_rest包和MySQL中的Doctrine来过滤对象。 Let's say I have a Master Entity, which has relations with different entities which have some properties.
假设我有一个主实体,它与具有某些属性的不同实体有关系。 Now in frontend I would like to create a filter, where one can filter the Master Entity by the properties of the related Entities.
现在在前端我想创建一个过滤器,其中可以通过相关实体的属性过滤主实体。 How would that be doable?
怎么会这样可行?
say we have 说我们有
+---------------+
| Master Entity |
+----+----------+
| id | name |
+----+----------+
| 1 | Apple |
+----+----------+
| 3 | Berry |
+----+----------+
+-------------------+
| Property Entity |
+----+------+-------+
| id | id_m | value |
+----+------+-------+
| 1 | 1 | green |
+----+------+-------+
| 2 | 1 | yello |
+----+------+-------+
| 3 | 1 | red |
+----+------+-------+
| 4 | 3 | pink |
+----+------+-------+
And I want to have a filter, where I filter by the values in Property Entity 我希望有一个过滤器,我按照Property Entity中的值进行过滤
I would like to do something like 我想做点什么
$em->getRepository('AcmeBundle\MasterEntity')->findBy(array("PropertyEntity:value" => "red","PropertyEntity:value" => "yello"))
so it would return the object collection of Master Entity with ID=1 (apple) - because both parameters would match Apple 所以它将返回ID为1(苹果)的主实体的对象集合 - 因为这两个参数都匹配Apple
You can create a query builder instead of use findBy 您可以创建查询生成器而不是使用findBy
$qb->select('m')
->from('AcmeBundle\MasterEntity', 'm')
->join('m.PropertyEntity', 'p')
->where('p.value IN (:values)')
->setParameter('values',['red','yellow']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.