原则2鉴别器继承映射导致findBy方法不起作用



我有两个实体,一个是由描述符等组成的主超类,另一个是扩展这个超类。。。这样我就可以将所有操作记录在一个名为Action的表中。

我的鉴别器实体:

namespace EntitiesMembers;
/**
 * @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 EntitiesMembers;
/**
 * @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");
    }
}

当我保留注释实体(例如)时,这很好

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

当我坚持时,它会成功地将操作添加到操作表中。。。

但是,我不能再使用任何findBy、findByOne方法,这些方法的任何结果的返回值都为null现在,当我编辑comments类并删除"extend from Action"时,条令findby、findOneBy方法开始工作但它不会添加到tmy鉴别器表中,因为它没有扩展主Actions超类。。。

我需要它来扩展Actions,并使用find、findOneBy等学说方法来工作。。。

有什么建议吗?

请发布您的查询代码。我很想看看这一切是如何运作的,所以我复制了你的实体,然后用进行了查询

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

看起来还不错。您是否试图使用Actions回购?

你可能不需要这个:

  • @MappedSuperClass

但拥有它似乎不会伤害任何东西。

最新更新