Inheritance 原则2鉴别器继承映射导致findBy方法不起作用
我有两个实体,一个是作为一个主要的超类,它由判别者等组成,另一个扩展了这个超类。。。这样我就可以 在一个名为Action的表中记录所有操作 我的歧视实体:Inheritance 原则2鉴别器继承映射导致findBy方法不起作用,inheritance,doctrine,mapping,doctrine-orm,discriminator,Inheritance,Doctrine,Mapping,Doctrine Orm,Discriminator,我有两个实体,一个是作为一个主要的超类,它由判别者等组成,另一个扩展了这个超类。。。这样我就可以 在一个名为Action的表中记录所有操作 我的歧视实体: namespace Entities\Members; /** * @Entity * @Table(name="actions") * @MappedSuperClass * @InheritanceType("JOINED") * @DiscriminatorColumn(name="action_type", type="s
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");
}
}
这是我的一个实体,我用它来扩展上述判别者实体:
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");
}
}
当我持久化comments实体时,这很好地工作
$entity = new Entities\Comments;
$entity->comment = "my new comment";
$this->em->persist($entity);
$this->em->flush();
当我坚持时,它会成功地将操作添加到操作表中
但是,我不能再使用任何findBy、findByOne方法,这些方法的任何结果的返回值都为null
现在,当我编辑comments类并删除'extend from Action'时,原则findby,findOneBy方法开始工作
但它不会添加到tmy鉴别器表中,因为它没有扩展主操作超类
我需要它来扩展行动,并使诸如find、findOneBy等学说方法也能起作用
有什么建议吗?请发布您的查询代码。我很想了解这一切是如何运作的,所以我复制了您的实体,然后使用以下工具进行了查询:
protected function testJoinedQuery()
{
$em = $this->getEntityManager();
$repo = $em->getRepository('Entity\Comments');
$entity = $repo->findOneBy(array('id' => 2));
echo get_class($entity) . ' ' . $entity->getComment() . "\n";
}
看起来效果不错。你是想用回购协议吗
您可能不需要这个:
- @映射超类
但拥有它似乎不会伤害任何东西。不会。。当我得到_class($entity)时,它会打印出代码所在的实际类名。。基站控制器?但是如果我删除“扩展活动”,它会打印出实体/注释,因此如果你不扩展操作,它就会工作。它与你的相同:$repo=$this->\u em->getRepository('Entities\Comments')$实体=$repo->findOneBy(数组('name'=>'bar')//$实体=$repo->findOneById(233);echo get_类($entity)。“”$实体->标识。“\n”;我两个都试过了,正如你在上面看到的。。。findOneById和findOneBy(数组)我认为id和扩展它的所有实体冲突。。。。(操作id字段)