@AssociationOverride and @AttributeOverride in new Doctrine 2.3

前端 未结 1 621
余生分开走
余生分开走 2021-01-13 09:26

As per title, what\'s the purpose of the new annotation @AssociationOverride and @AttributeOverride?

The only thing I can find on Doctrine

相关标签:
1条回答
  • 2021-01-13 09:53

    By looking at the code in the commit, we can see that it is used to override a field mapping already defined in a mapped superclass / trait.

    The tests included in the commit demonstrate this behaviour:

    Mapped superclass

    /** 
     * @MappedSuperclass
     */
    class User
    {
        /**
         * @Id
         * @GeneratedValue
         * @Column(type="integer", name="user_id", length=150)
         */
        protected $id;
    
        /**
         * @Column(name="user_name", nullable=true, unique=false, length=250)   
         */
        protected $name;
    
        /**
         * @var ArrayCollection
         *
         * @ManyToMany(targetEntity="Group", inversedBy="users", cascade={"persist", "merge", "detach"})
         * @JoinTable(name="users_groups",
         *  joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
         *  inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
         * )
         */
        protected $groups;
    
        /**
         * @var Address
         *
         * @ManyToOne(targetEntity="Address", cascade={"persist", "merge"})
         * @JoinColumn(name="address_id", referencedColumnName="id")
         */ 
        protected $address;
    
        ...
    }
    

    Subclass using @AssociationOverride

    /*  
     * @Entity
     * @AssociationOverrides({
     *      @AssociationOverride(name="groups",
     *          joinTable=@JoinTable(
     *              name="users_admingroups",
     *              joinColumns=@JoinColumn(name="adminuser_id"),
     *              inverseJoinColumns=@JoinColumn(name="admingroup_id")
     *          )
     *      ),
     *      @AssociationOverride(name="address",
     *          joinColumns=@JoinColumn(
     *              name="adminaddress_id", referencedColumnName="id"
     *          )
     *      )   
     * })
     */
    class Admin extends User
    {
        ...
    }
    

    Subclass using @AttributeOverride

    /**
     * @Entity
     * @AttributeOverrides({
     *      @AttributeOverride(name="id",
     *          column=@Column(
     *              name     = "guest_id",
     *              type     = "integer",
     *              length   = 140
     *          )
     *      ),
     *      @AttributeOverride(name="name",
     *          column=@Column(
     *              name     = "guest_name",
     *              nullable = false,
     *              unique   = true,
     *              length   = 240
     *          )
     *      )   
     * })
     */
    class Guest extends User
    {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题