问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9534162/doctrine-2-discriminator-inheritance-mapping-leads-to-findby-methods-not-working