[Doctrine] [Symfony]单个注释请求中的多个连接(可能吗?)

时间:2018-04-17 11:32:17

标签: php symfony doctrine symfony-3.4

我正在研究Symfony项目(V3.4),而我正在使用现有的数据库(无法更改)。 为了与它交互,我使用了学说注释,工作做得很好! 我设法使用JoinTable和JoinColumns提交请求,但最后一件事我不知道如何处理......

我有以下表格:

tables

我有来自表A的id,我正试图从E表中获取libelle。

有没有办法使用注释来做?现在我已经在3个表之间完成了它,但我不知道如何做更多:

-fdump-tree-cfg-lineno

如果不可能,我愿意接受建议。

谢谢!

1 个答案:

答案 0 :(得分:0)

通过查看您的需求和架构,我可以告诉您,您不需要多对多的关系。您应该跨实体(表)进行双向关系。

我不知道你现在有哪种关系,但假设一对一,你可以按照以下方式设置关系:

<强> EntityA

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

<强> EntityB

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

<强> EntityC

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

<强> EntityD

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

<强> EntityE

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

我没时间测试它。但它应该是直截了当的。

您可以通过以下方式从E表中获取libelle:

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