新的Doctrine 2.3中的@AssociationOverride和@AttributeOverride

时间:2012-09-14 08:07:50

标签: doctrine doctrine-orm

根据标题,新注释@AssociationOverride@AttributeOverride的目的是什么?

我在Doctrine网站上唯一能找到的是:

  

@AssociationOverride和@AttributeOverride(对于Trait和   MappedSuperclass)

1 个答案:

答案 0 :(得分:5)

通过查看commit中的代码,我们可以看到它用于覆盖已在映射的超类/特征中定义的字段映射。

提交中包含的测试证明了这种行为:

映射的超类

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

    ...
}

使用@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
{
    ...
}

使用@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
{
    ...
}