Doctrine 2鉴别器继承映射导致findBy方法不起作用 [英] Doctrine 2 Discriminator Inheritance Mapping leads to findBy methods not working
问题描述
记录一个名为Action的表中的所有操作。
我的鉴别器实体:
命名空间Entities\Members;
/ **
* @Entity
* @Table(name =actions)
* @MappedSuperClass
* @InheritanceType(JOINED )
* @DiscriminatorColumn(name =action_type,type =string)
* @DiscriminatorMap({comments=评论,博客=博客})
* @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);
}
}
这是我的实体,我用于扩展上述辨别器实体:
命名空间Entities\Members;
/ **
* @Entity
* @Table(name =comments)
* @HasLifecycleCallbacks
* /
class评论extends Action {
/ **
* @Id @Column(name =id,type =bigint,length = 15)
* @GeneratedValue =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 Entities\Comments;
$ entity-> comment =我的新评论;
$ this-> em-> persist($ entity);
$ this-> em-> flush();
当我坚持,它将动作成功添加到动作表...
然而,我不能使用任何findBy,findByOne方法,这些方法的任何结果的返回值= null
现在,当我编辑评论类并删除'从Action'延伸,doctrine findby,findOneBy方法开始工作
,但它不会添加到tmy鉴别表,因为它不扩展主要的Action超类...
我需要它来扩展动作,并且使用诸如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()。 \\\
;
}
似乎工作正常。您是否尝试使用Actions repo?
您可能不需要这样:
- @MappedSuperClass
但是它似乎没有什么伤害。
i have 2 entities, one as a main super class which consist of the discrimators etc and one which extends this super class... so that i can record all actions in one table called Action.
my discrimator entity:
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");
}
}
this is one my entities that i use to extend the above discrimator entity:
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");
}
}
this works nicely when i persist the comments entity, e.g.
$entity = new Entities\Comments;
$entity->comment = "my new comment";
$this->em->persist($entity);
$this->em->flush();
when i persist, it adds the action successfully into the actions table...
however, i cannot use any findBy, findByOne methods anymore,, the return value of any result from these methods = null now, when i edit the comments class and remove the 'extend from Action', the doctrine findby, findOneBy methods starts working but it wont add to tmy discriminator table as its not extending that main Actions super class...
i need it to extend Actions and have the doctrine method such as find, findOneBy, etc to work also...
any suggestions?
Please post your query code. I was interested in seeing how this all worked so I copied your entities then did a query with:
protected function testJoinedQuery()
{
$em = $this->getEntityManager();
$repo = $em->getRepository('Entity\Comments');
$entity = $repo->findOneBy(array('id' => 2));
echo get_class($entity) . ' ' . $entity->getComment() . "\n";
}
Seemed to work just fine. Were you trying to use the Actions repo?
You probably won't need this:
- @MappedSuperClass
But having it doesn't seem to hurt anything.
这篇关于Doctrine 2鉴别器继承映射导致findBy方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!