使用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.

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...