symfony2 / doctrine2中的多级getter

时间:2022-05-16 06:47:48

Im wondering if theres a cleaner way to approach the system i have below, using symfony2/doctrine2.

我想知道如果使用symfony2 / doctrine2,我有一个更清洁的方式来接近下面的系统。

I currently have three entities

我目前有三个实体

Entity A - many to one relationship with class B, one to many with class C Entity B - one to many relationship with class A, one to many with class C.
Entity C - Many to one relationship with class A and B.

实体A - 与B类的多对一关系,与C类实体B的一对多关系 - 与A类的一对多关系,与C类的一对多关系。实体C - 与A类和B类的多对一关系

If i do $entityA->getEntityB()->getEntityC() that will return me all the C entities assigned to entity B, but what i actually want is all the entity C entities that are assigned to both entity B and entity A. Essentially i want to recognise the getter chain, if that makes sense.

如果我执行$ entityA-> getEntityB() - > getEntityC(),它将返回分配给实体B的所有C实体,但我真正想要的是分配给实体B和实体A的所有实体C实体。基本上我想要认识到吸气链,如果这是有道理的。

At the moment i have to pass entity A into the getEntityC method and filter out the values i dont want which is starting to get a little messy when dealing with more objects and other parts of code.

目前,我必须将实体A传递给getEntityC方法并过滤掉我不想要的值,这些值在处理更多对象和代码的其他部分时开始变得有点混乱。

Is there a way to set this up whereby the last getter will force the relationship from both parents instead of just the immediate one?

有没有办法设置这个,最后一个吸气剂将迫使父母双方的关系,而不仅仅是直接的?

Any help would be much appreciated.

任何帮助将非常感激。

1 个解决方案

#1


0  

I would much recommend not to try to get value by getters, but instead create a query by DoctrineQueryBuilder and use multiple left joins. For example something like this:

我建议不要试图通过getter获取值,而是通过DoctrineQueryBuilder创建一个查询并使用多个左连接。例如这样的事情:

$repository = $this->getDoctrine()->getRepository('GreatBundle:Entity');
$q = $repository->createQueryBuilder('e1', 'e2', 'e3')->leftJoin('e1.e2', 'e2')->leftJoin('e1.e3', 'e3');
$columns = array(
    'e1.id',
    'e2.id as e2_id',
    'c3.id as e3_id',
);
$result = $q->select($columns)->getQuery()->getArrayResult();

Sorry that I didn't fully get your usage case, but you will have to recreate query just to fit it.

很抱歉,我没有完全了解您的使用案例,但您必须重新创建查询才能适应它。

#1


0  

I would much recommend not to try to get value by getters, but instead create a query by DoctrineQueryBuilder and use multiple left joins. For example something like this:

我建议不要试图通过getter获取值,而是通过DoctrineQueryBuilder创建一个查询并使用多个左连接。例如这样的事情:

$repository = $this->getDoctrine()->getRepository('GreatBundle:Entity');
$q = $repository->createQueryBuilder('e1', 'e2', 'e3')->leftJoin('e1.e2', 'e2')->leftJoin('e1.e3', 'e3');
$columns = array(
    'e1.id',
    'e2.id as e2_id',
    'c3.id as e3_id',
);
$result = $q->select($columns)->getQuery()->getArrayResult();

Sorry that I didn't fully get your usage case, but you will have to recreate query just to fit it.

很抱歉,我没有完全了解您的使用案例,但您必须重新创建查询才能适应它。