have additional column in ManyToMany join table in Doctrine (Symfony2)

后端 未结 1 1319
故里飘歌
故里飘歌 2021-01-03 11:53

Situation

I have two entities User and Group with relation ManyToMany. Relation is created as separated table (called user_group) with two columns

1条回答
  •  生来不讨喜
    2021-01-03 12:16

    No you can't, to have ManyToMany association and additional column in your junction table you need to rewrite your mapping as to have a junction entity between your user and group entity as below

    For user your mapping will be like

            OneToMany
          ------------>
    User          UserHasGroups
          <------------
            ManyToOne
    

    Same for group entity

            OneToMany
           ----------->
    Group          UserHasGroups
           <-----------
            ManyToOne
    

    Now you can set your entities as

    User Entity

    /**
     * User
     * @ORM\Table(name="user_table_name")
     * @ORM\Entity
     */
    class User
    {
        /**
         * @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\UserHasGroups", mappedBy="users",cascade={"persist","remove"} )
         */
        protected $hasGroups;
    
    }
    

    Group Entity

    /**
     * Group
     * @ORM\Table(name="group_table_name")
     * @ORM\Entity
     */
    class Group 
    {
        /**
         * @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\UserHasGroups", mappedBy="groups",cascade={"persist","remove"} )
         */
        protected $hasUsers;
    
    }
    

    UserHasGroups Entity

    /**
     * UserHasGroups
     * @ORM\Table(name="user_groups_table_name")
     * @ORM\Entity
     */
    class UserHasGroups
    {
    
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Group", cascade={"persist"}, fetch="LAZY")
         * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
         */
        protected $groups;
    
        /**
         * @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\User", cascade={"persist","remove"} ,inversedBy="medias", fetch="LAZY" )
         * @ORM\JoinColumn(name="user_id", referencedColumnName="id",nullable=true)
         */
        protected $users;
    
    
        /**
         * @var \DateTime
         * @ORM\Column(name="added_on", type="datetime")
         */
        protected $addedOn;
    
        public function __construct()
        {
            $this->addedOn= new \DateTime('now');
        }
    
    }
    

    Now you have mapped your entities you can have your UserHasGroups entity use repository method by provide user and group values like findBy,findAll etc or if you have user object then you can directly get the UserHasGroups object which contains the collection of associations for that user

    0 讨论(0)
提交回复
热议问题