Extending entities in Symfony2 with Doctrine2

单表继承配置
本文介绍了如何使用Doctrine进行单表继承的配置,通过示例展示了如何定义基类和派生类,以及如何设置注解来区分不同类型的记录。

down vote

You can see in this link to know about inheritance: http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html#single-table-inheritance

You must declare in UserBundle\Entity\User:

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"baseuser" = "UserBundle\Entity\User", "blogUser" = "BlogBundle\Entity\User"})
 */
class User implements UserInterface
{
    ...
}

And BlogBundle\Entity\User

use App\UserBundle\Entity\User as BaseUser;
/**
 * @ORM\Entity
 */
class User extends BaseUser
{
    ....
}

Goodluck!

 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

http://stackoverflow.com/questions/5823905/how-to-manage-single-table-inheritance-within-doctrine-2

--------------------------------------------------------

Based on the example from the docs:

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="resource_type", type="string")
 * @DiscriminatorMap({"article_vote" = "ArticleVote", "comment_vote" = "CommentVote"})
 */
class Vote
{
    private $id;
    private $weight;
}

class ArticleVote extends Vote
{
    /** @ManyToOne(...) */
    private $article;
}

class CommentVote extends Vote
{
    /** @ManyToOne(...) */
    private $comment;
}

shareimprove this answer

edited Apr 29 '11 at 14:23

answered Apr 29 '11 at 3:53

 

Just incase someone else needs it, here is an detailed example in using Table Inheritance with Doctrine. I found it more informative than the Doctrine documentation:

http://blog.liip.ch/archive/2012/03/27/table-inheritance-with-doctrine.html

转载于:https://my.oschina.net/u/144160/blog/775057

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值