【问题标题】:Doctrine 2 Discriminator Inheritance Mapping leads to findBy methods not workingDoctrine 2 Discriminator Inheritance Mapping 导致 findBy 方法不起作用
【发布时间】:2012-03-21 00:10:34
【问题描述】:

我有 2 个实体,一个作为主要超类,由鉴别器等组成,一个扩展了这个超类......所以我可以 将所有动作记录在一个名为 Action 的表中。

我的鉴别实体:

namespace Entities\Members;

/**
 * @Entity
 * @Table(name="actions")
 * @MappedSuperClass
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="action_type", type="string")
 * @DiscriminatorMap({"comments" = "Comments", "blog" = "Blog"})
 * @HasLifecycleCallbacksIndex
 */
class Action {

    /**
     * @Id 
     * @Column(name="id", type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @Column(type="string", length=300, nullable=true) */
    public $name;

    /** @Column(name="action_date", type="datetime", columnDefinition="datetime", nullable=false) */
    protected $action_date;

    /** @PrePersist */
    public function updated() {
        $this->action_date = new \DateTime("now");
    }

}

这是我用来扩展上述鉴别器实体的一个实体:

namespace Entities\Members;

/**
 * @Entity
 * @Table(name="comments")
 * @HasLifecycleCallbacks
 */
class Comments extends Action {

    /**
     * @Id @Column(name="id", type="bigint",length=15)
     * @GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @Column(name="blog_id", type="integer", nullable=true) */
    protected $blog_id;

    /** @Column(name="comment", type="string", length=255, nullable=true) */
    protected $comment;

    /** @Column(name="comment_date", type="datetime", columnDefinition="datetime", nullable=true) */
    protected $comment_date;

    /**
     * @ManyToOne(targetEntity="Members",inversedBy="comments", cascade={"persist"})
     * @JoinColumn(name="userid", referencedColumnName="id")
     */
    protected $author;


    public function __construct() {
        $this->comment_date = $this->comment_date = new \DateTime("now");
    }

}

当我坚持 cmets 实体时,这很好用,例如

$entity = new Entities\Comments;
$entity->comment = "my new comment";
$this->em->persist($entity);
$this->em->flush();

当我坚持时,它成功地将动作添加到动作表中......

但是,我不能再使用任何 findBy、findByOne 方法,这些方法的任何结果的返回值 = null 现在,当我编辑 cmets 类并删除“从动作扩展”时,学说 findby、findOneBy 方法开始工作 但它不会添加到 tmy 鉴别器表中,因为它没有扩展主要的 Actions 超类......

我需要它来扩展 Actions 并让诸如 find、findOneBy 等教义方法也可以工作......

有什么建议吗?

【问题讨论】:

    标签: inheritance doctrine mapping doctrine-orm discriminator


    【解决方案1】:

    请发布您的查询代码。我有兴趣了解这一切是如何工作的,所以我复制了您的实体,然后进行了查询:

    protected function testJoinedQuery()
    {
        $em = $this->getEntityManager();
        $repo = $em->getRepository('Entity\Comments');
    
        $entity = $repo->findOneBy(array('id' => 2));
    
        echo get_class($entity) . ' ' . $entity->getComment() . "\n";
    
    }
    

    似乎工作得很好。您是否尝试使用 Actions 存储库?

    你可能不需要这个:

    • @MappedSuperClass

    但拥有它似乎并没有伤害任何东西。

    【讨论】:

    • nope.. 当我执行 get_class($entity) 时,它会打印出此代码所在的实际类名。 BaseController ?但是如果我删除“扩展活动”,它会打印出实体/评论,所以如果你不扩展操作它就可以工作..
    • 和你的一样: $repo = $this->_em->getRepository('Entities\Comments'); $entity = $repo->findOneBy(array('name' => 'bars')); //$entity = $repo->findOneById(233);回声 get_class($entity) 。 ' ' 。 $实体->id 。 "\n";正如您在上面看到的那样,我都尝试了... findOneById 和 findOneBy(array) 我认为它的 id 与扩展它的所有实体发生冲突....(Actions id 字段)
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    相关资源
    最近更新 更多