Php Symfony3条令单表固有SQL状态[23000]:完整性约束冲突
我是新来的Symfony,我正在尝试做一个简单的博客。我的数据库中有用户作为注释的作者和两种类型的注释——PostComment和ReplyComment,它们都扩展了抽象类注释。我正在尝试将注释保存到DB,但遇到以下错误: 执行“插入注释文本”时发生异常, 作者id、帖子id、注释类型值?、?、?、?'以及参数 [Lorem ipsum,1,1,发表评论]: SQLSTATE[23000]:完整性约束冲突:1452无法添加或 更新子行:外键约束失败 blog_symfony.comment,约束FK_9474526CDB1174D2国外 关键帖子注释id引用注释id 这是抽象注释类:Php Symfony3条令单表固有SQL状态[23000]:完整性约束冲突,php,mysql,doctrine-orm,doctrine,symfony,Php,Mysql,Doctrine Orm,Doctrine,Symfony,我是新来的Symfony,我正在尝试做一个简单的博客。我的数据库中有用户作为注释的作者和两种类型的注释——PostComment和ReplyComment,它们都扩展了抽象类注释。我正在尝试将注释保存到DB,但遇到以下错误: 执行“插入注释文本”时发生异常, 作者id、帖子id、注释类型值?、?、?、?'以及参数 [Lorem ipsum,1,1,发表评论]: SQLSTATE[23000]:完整性约束冲突:1452无法添加或 更新子行:外键约束失败 blog_symfony.comment,约
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()
]);
}
首先,基类不需要是抽象的。相反,您必须在类上方插入此注释,以便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
{
这应该会有帮助然后您可以检查答案