简体   繁体   English

[Doctrine] [Symfony]在单个注释请求中进行多个联接(可能吗?)

[英][Doctrine][Symfony] Multiple Joins in a single annotation request (is it possible ?)

I'm working on a Symfony project (V3.4) and I'm using an existing DB (can't change it). 我正在开发一个Symfony项目(V3.4),并且正在使用现有的数据库(无法更改它)。 To interact with it I use doctrine annotations and the work is well done! 为了与之交互,我使用了学说注释,并且工作做得很好! I manage to submit requests using JoinTable and JoinColumns but there is one last thing I don't know how to deal with ... 我设法使用JoinTable和JoinColumns提交请求,但是最后一件事我不知道该如何处理...

I have the following tables : 我有以下表格:

tables 桌子

I have the id from table A and I'm trying to get the libelle from the E table. 我有来自表A的ID,并且正在尝试从E表中获取诽谤。

Is there a way to do it using annotations? 有没有办法使用注释? For now I've already done it between 3 tables but I don't know how to do it for more : 到目前为止,我已经在3个表之间完成了此操作,但我不知道如何做更多操作:

@ORM\ManyToMany(targetEntity="")
 * @ORM\JoinTable(name="",joinColumns={@ORM\JoinColumn(
 name="",referencedColumnName="")}, inverseJoinColumns={@ORM\JoinColumn(
 name="",referencedColumnName="", unique=true)})

If it's not possible I'm open to suggestions. 如果不可能,我欢迎您提出建议。

Thanks! 谢谢!

By looking at your need and your schema, i can tell you that you don't need many to many relationship. 通过查看您的需求和架构,我可以告诉您您不需要多对多的关系。 You should bidirectional relation across the entities(tables). 您应该跨实体(表)建立双向关系。

I don't know which kind of relation you have right now, but assuming one-to-one, you can setup relations following way: 我不知道您现在具有哪种关系,但是假设一对一,您可以按照以下方式设置关系:

EntityA 实体A

/**
 * @OneToOne(targetEntity="EntityB", mappedBy="entityA")
 */
private $entityB;

EntityB 实体B

 /**
 * @OneToOne(targetEntity="EntityA", inversedBy="entityB")
 * @JoinColumn(name="entityA_id", referencedColumnName="id")
 */
private $entityA;
/**
 * @OneToOne(targetEntity="EntityC", mappedBy="entityB")
 */
private $entityC;

EntityC 实体C

/**
 * @OneToOne(targetEntity="EntityB", inversedBy="entityC")
 * @JoinColumn(name="entityB_id", referencedColumnName="id")
 */
private $entityB;
/**
 * @OneToOne(targetEntity="EntityD", mappedBy="entityC")
 */
private $entityD;

EntityD 实体D

 /**
 * @OneToOne(targetEntity="EntityC", inversedBy="entityD")
 * @JoinColumn(name="entityC_id", referencedColumnName="id")
 */
private $entityC;
/**
 * @OneToOne(targetEntity="EntityE", mappedBy="entityE")
 */
private $entityE;

EntityE 实体E

 /**
 * @OneToOne(targetEntity="EntityD", inversedBy="entityE")
 * @JoinColumn(name="entityD_id", referencedColumnName="id")
 */
private $entityD;

I didn't have time to test it. 我没有时间进行测试。 But it should be straight forward. 但这应该是直截了当的。

You can get libelle from E table following way: 您可以通过以下方式从E表获得诽谤:

$entityA = $entityRepositoryForA->find(a_id_here);
$entityA->getEntityB()->getEntityC()->getEntityD()->getEntityE->getLibelle();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM