Symfony3学说单表无罪SQLSTATE [23000]:完整性约束违规
问题描述:
我是Symfony的新手,我正在尝试做一个简单的Blog。我有我的数据库中的用户作为评论的作者和两种类型的评论 - PostComment和ReplyComment,它们都扩展了抽象类的评论。我想评论保存到数据库,但我坚持了这个错误:Symfony3学说单表无罪SQLSTATE [23000]:完整性约束违规
发生异常而执行“INSERT INTO评论(文字, AUTHOR_ID,POST_ID,comment_type)VALUES(,,??? ?)”使用参数 [ “Lorem存有”,1,1, “post_comment”]:
SQLSTATE [23000]:完整性约束违规:1452不能添加或 更新子行,外键约束失败 (
blog_symfony
。comment
,CONSTRAINTFK_9474526CDB1174D2
FOREIGN KEY(post_comment_id
)参考文献comment
(id
))
这是抽象的注释类别:
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;
}
}
最后这是在控制器运行过程中出现的逻辑
代码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()
]);
}
答
At首先你的基类不需要抽象。相反,你必须插入此标注类以上,这样的学说会得到它:
@MappedSuperclass()
remove从基础机构的所有其他学说的注释,它们都属于实体类。
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
{
这应该有助于
它没有,感谢ü:) –
那么你可以检查答案 – Rawburner
我的意思是接受它 – Rawburner