Doctrine 2 Discriminator Inheritance Mapping leads to findBy methods not working

ε祈祈猫儿з 提交于 2019-12-12 05:37:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!