Я новичок в Symfony, и я стараюсь сделать простой блог. У меня есть пользователи в моей базе данных как авторы комментариев и 2 типа комментариев - PostComment и ReplyComment, которые расширяют абстрактный класс. Комментарии. Я пытаюсь сохранить комментарий к БД, но я застрял с этой ошибкой:Symfony3 Doctrine Single Table Inhertiance SQLSTATE [23000]: Нарушение ограничения целостности
Исключения при выполнении «INSERT INTO комментария (текст, AUTHOR_ID, POST_ID, comment_type) VALUES (,?? ?,)»с Params [ "Lorem Ipsum", 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;
}
}
И, наконец, это код в контроллере 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()
]);
}
Он сделал, спасибо u :) –
, то вы можете проверить ответ – Rawburner
я имею в виду принять его – Rawburner