Symfony3 Doctrine单表不一致SQLSTATE [23000]:完整性约束违规

时间:2016-11-30 14:52:36

标签: php mysql doctrine-orm doctrine symfony

我是Symfony的新手,我正在尝试做一个简单的博客。我的数据库中有用户作为评论的作者和2种评论类型 - PostComment和ReplyComment都扩展了抽象类注释。我正在尝试将注释保存到数据库,但我遇到了这个错误:

  

执行'INSERT INTO评论时发生异常(文字,   author_id,post_id,comment_type)VALUES(?,?,?,?)'与params   [“Lorem ipsum”,1,1,“post_comment”]:

     

SQLSTATE [23000]:完整性约束违规:1452无法添加或   更新子行:外键约束失败   (blog_symfonycomment,CONSTRAINT FK_9474526CDB1174D2外国   KEY(post_comment_id)参考commentid))

这是抽象的评论类:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="comment")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="comment_type", type="string")
 * @ORM\DiscriminatorMap({"post_comment" = "PostComment", "reply_comment" = "ReplyComment"})
 */
abstract class Comment
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="userComments")
     */
    protected $author;

    /**
     * @ORM\Column(type="string")
     */
    protected $text;

    /**
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

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

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

    /**
     * @return string $text
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * @param string $text
     */
    public function setText($text)
    {
        $this->text = $text;
    }
}

这是一个帖子评论类

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostCommentRepository")
 */
class PostComment extends Comment
{
    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Post", inversedBy="comments")
     * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
     */
    private $post;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\ReplyComment", mappedBy="postComment", cascade={"remove"}, orphanRemoval=true)
     * @ORM\OrderBy({"id"="DESC"})
     */

    private $replyComments;

    /**
     * @return replyComment[] reply comments
     */
    public function getReplyComments()
    {
        return $this->replyComments;
    }

    /**
     * @param replyComment[] reply comments
     */
    public function setReplyComments($replyComments)
    {
        $this->replyComments = $replyComments;
    }

    /**
     * @return Post post
     */
    public function getPost()
    {
        return $this->post;
    }

    /**
     * @param Post post
     */
    public function setPost($post)
    {
        $this->post = $post;
    }
}

最后这是控制器runnig逻辑中的代码

if ($postCommentForm->isSubmitted() && $postCommentForm->isValid())
        {
            /** @var PostComment $comment */
            $comment = $postCommentForm->getData();
            $comment->setPost($post);

            $author = $this->getDoctrine()->getRepository('AppBundle:User')->findOneBy([
                'email' => $comment->getAuthor()
            ]);

            $comment->setAuthor($author);

            $em = $this->getDoctrine()->getManager();
            $em->persist($comment);
            $em->flush();

            return $this->redirectToRoute("single_post", [
                'id' => $post->getId()
            ]);
        }

1 个答案:

答案 0 :(得分:2)

首先,您的基类不需要是抽象的。相反,你必须在类上面插入这个注释,因此doctrine将得到它:

@MappedSuperclass()

从基础实体中删除所有其他学说注释,它们都属于实体类。

use Doctrine\ORM\Mapping as ORM;

/**
 *@MappedSuperclass()
 */
class Comment
{

和实体具有所有其他注释:

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostCommentRepository")
 * @ORM\Table(name="comment")
 */
class PostComment extends Comment
{

这应该有帮助