dongre9937 2018-04-17 11:32
浏览 41
已采纳

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

I'm working on a Symfony project (V3.4) and I'm using an existing DB (can't change it). 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 ...

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.

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 :

@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!

  • 写回答

1条回答 默认 最新

  • douhuang2673 2018-04-17 12:43
    关注

    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

    /**
     * @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;
    

    I didn't have time to test it. But it should be straight forward.

    You can get libelle from E table following way:

    $entityA = $entityRepositoryForA->find(a_id_here);
    $entityA->getEntityB()->getEntityC()->getEntityD()->getEntityE->getLibelle();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决