5

访问原则 2 中的代理类属性时出现 EntityNotFoundException 的可能原因是什么?无论如何,以下是我的实体的结构:

/**
 * @ORM\Table(name="comments")
 *
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="comment_type", type="smallint")
 * @ORM\DiscriminatorMap({
 *    1 = "VisitorComment",
 *    2 = "MemberComment"
 * })
 */
 class Comment
 {
    //with common properties of its subclasses
 }

子类如下:

/**
 * @ORM\Table(name="member_comments")
 */
class MemberComment extends Comment
{
    /**
     * owning side
     *
     * @var Member $author
     * 
     * @ORM\ManyToOne(targetEntity="Member")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
     */
     private $author;

    /**
     * Set author
     *
     * @param Member $author
     */
    public function setAuthor($author)
    {
        $this->author = $author;
    }

    /**
     * Get author
     *
     * @return Member
     */
    public function getAuthor()
    {
        return $this->author;
    }
}


/**
 * @ORM\Table(name="visitor_comments")
 */
class VisitorComment extends Comment
{
   /**
    * owning side
    *
    * @var Visitor $author
    * 
    * @ORM\ManyToOne(targetEntity="Visitor")
    * @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
    */
    private $author;

    /**
     * Set author
     *
     * @param string $author
     */
     public function setAuthor($author)
     {
        $this->author = $author;
     }

    /**
     * Get author
     *
     * @return Visitor
     */
    public function getAuthor()
    {
        return $this->author;
    }
}

当我调用 $comment->getAuthor()->getFirstName() <假设是 Member 或 Visitor 实体的作者具有 firstName 属性时,会发生异常。在这种情况下,getAuthor() 返回一个 VisitorProxy 或 MemberProxy 的代理类。

请帮助我。我对教义还是新手。

4

2 回答 2

2

正如 Floricel 发现的那样,这可能是由指向 Proxy 类引用的表的列中的无效外键引起的。

于 2013-06-04T16:33:29.057 回答
0

@Dave Lancea 是对的,我将 FK 更改为非 Null,然后开始出现此错误,手动更新了损坏的记录,使其指向现有实体并且问题消失了。

于 2013-09-14T05:05:21.463 回答