dsh7623 2015-06-08 13:56
浏览 52
已采纳

使用doctrine ORM(Symfony)正确创建ManyToMany递归/反身关系

I have this entity named Pointscomptage.php:

class Pointscomptage

    {
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;

        /**
         * @var string
         *
         * @ORM\Column(name="invariant", type="string", length=150, nullable=false)
         */
        private $invariant;

        /**
         * @var string
         *
         * @ORM\Column(name="nom", type="string", length=150, nullable=false)
         */
        private $nom;

        /**
         * @var string
         *
         * @ORM\Column(name="consoProduction", type="string", length=150, nullable=false)
         */
        private $consoProduction;

        /**
         * @var Typesenergie
         *
         * @ORM\ManyToOne(targetEntity="Typesenergie", inversedBy="pointscomptage")
         * @ORM\JoinColumn(name="typesenergie_id", referencedColumnName="id")
         */
        private $typesenergie;

        /** ... getters and setters */

I need to understand how to create a ManyToMany recursive or reflexive relation on this entity.

That is to say, a Pointscomptage could have no (0) or many (n) Pointscomptage.

How to make this relation on this same entity?


EDIT

Thanks to Jovan Perovic answer and suggestion this the solution I found, we need to respect doctrine annotation:

/**
     * @ORM\ManyToMany(targetEntity="Pointscomptage")
     * @ORM\JoinTable(name="pointscomptage_link_table",
     * joinColumns={
     *     @ORM\JoinColumn(name="pointscomptage_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="id_pointscomptage2", referencedColumnName="id")
     *   }
     * )
     **/
    private $pointscomptages;

Thank you a lot.

  • 写回答

1条回答 默认 最新

  • douzhenchun6782 2015-06-08 14:15
    关注

    That can be done this same way as with any entity. See official docs on @ManyToMany

    For example:

    /**
     * @ManyToMany(targetEntity="Pointscomptage")
     * @JoinTable(name="pointscomptage_link_table",
     *      joinColumns={@JoinColumn(name="id_pointscomptage1", referencedColumnName="id")},
     *      inverseJoinColumns={@JoinColumn(name="id_pointscomptage2", referencedColumnName="id")}
     *      )
     **/
    private $pointscomptages;
    

    This example assumes the following:

    • The relation is unidirectional. Bidirectional is just as easy achieved and is described as well in the docs link above.
    • Connecting table is named pointscomptage_link_table
    • Connecting table pointscomptage_link_table contains two column keys:
      • id_pointscomptage1
      • id_pointscomptage2

    Hope this helps a bit...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算