我是Symfony的新手,我正在尝试做一个简单的博客。我的数据库中有用户是评论的作者和两种评论类型的作者 - 邮政和答复,都扩展了抽象的类评论。我试图将评论保存到数据库,但我遇到了这个错误:
在执行注释中插入评论时发生了一个例外(文本, ruther_id,post_id,comment_type)value(?,?,?,?)'with params' [" lorem ipsum",1,1," post_comment"]:
SQLSTATE [23000]:违规完整性约束:1452无法添加或 更新子行:外键约束失败 (
blog_symfony
。comment
,约束FK_9474526CDB1174D2
外国 键(post_comment_id
)参考comment
(id
))
这是抽象评论类:
use DoctrineORMMapping as ORM;
/**
* @ORMEntity()
* @ORMTable(name="comment")
* @ORMInheritanceType("SINGLE_TABLE")
* @ORMDiscriminatorColumn(name="comment_type", type="string")
* @ORMDiscriminatorMap({"post_comment" = "PostComment", "reply_comment" = "ReplyComment"})
*/
abstract class Comment
{
/**
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
* @ORMColumn(type="integer")
*/
protected $id;
/**
* @ORMManyToOne(targetEntity="AppBundleEntityUser", inversedBy="userComments")
*/
protected $author;
/**
* @ORMColumn(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 DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppBundleRepositoryPostCommentRepository")
*/
class PostComment extends Comment
{
/**
* @ORMManyToOne(targetEntity="AppBundleEntityPost", inversedBy="comments")
* @ORMJoinColumn(nullable=true, onDelete="SET NULL")
*/
private $post;
/**
* @ORMOneToMany(targetEntity="AppBundleEntityReplyComment", mappedBy="postComment", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"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 Logic
中的代码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()
]);
}
首先,您的基类无需抽象。取而代
@MappedSuperclass()
从基本实体中删除所有其他学说注释,它们都属于实体类。
use DoctrineORMMapping as ORM;
/**
*@MappedSuperclass()
*/
class Comment
{
和实体都有所有其他注释:
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppBundleRepositoryPostCommentRepository")
* @ORMTable(name="comment")
*/
class PostComment extends Comment
{
这应该有所帮助