gpt4 book ai didi

php - 如何将父实体传递给 Symfony 中的表单?

转载 作者:可可西里 更新时间:2023-11-01 12:36:45 24 4
gpt4 key购买 nike

假设我有两个实体:postcomment。每个 post 可以有很多 comments。现在,假设我有一个评论表。它应该接受用户输入并将其存储在数据库中。

简单的东西。至少,它应该是,但我无法让它工作。

如何在创建评论(子)时引用帖子(父)?我尝试手动将 post_id 作为隐藏字段传递给评论表单,但收到一个错误,提示帖子 ID 是一个字符串。

Expected argument of type "App\Entity\Post or null", "string" given.

到目前为止,这是我的代码。有人可以将我推向正确的方向吗?

CommentType.php

public function buildForm(FormBuilderInterface $builder, array $options)
{
$post_id = $options['post_id'];

$builder->add('content', TextareaType::class, [
'constraints' => [
new Assert\NotBlank(['message' => 'Your comment cannot be blank.']),
new Assert\Length([
'min' => 10,
'minMessage' => 'Your comment must be at least {{ limit }} characters long.',
]),
],
])->add('post', HiddenType::class, ['data' => $post_id]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Comment::class,
'post_id' => NULL,
]);
}

PostController.php(这是评论表单出现的地方)

// Generate the comment form.
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment, [
'action' => $this->generateUrl('new_comment'),
'post_id' => $post_id,
]);

CommentController.php

/**
* @param Request $request
* @Route("/comment/new", name="new_comment")
* @return
*/
public function new(Request $request, UserInterface $user)
{
// 1) Build the form
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);

// 2) Handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
// 3) Save the comment!
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($comment);
$entityManager->flush();
}

return $this->redirectToRoute('homepage');
}

非常感谢您的帮助!

最佳答案

您只需要传递实际的 Post 实体,而不仅仅是 id。试试这个:

CommentController.php

public function new(Request $request, UserInterface $user, Post $post)
{
// 1) Build the form
$comment = new Comment();
$comment->setPost($post); //where $post is instance of App\Entity\Post
$form = $this->createForm(CommentType::class, $comment);

// 2) Handle the submit (will only happen on POST)
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
// 3) Save the comment!
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($comment);
$entityManager->flush();
}

return $this->redirectToRoute('homepage');
}

评论类型

public function buildForm(FormBuilderInterface $builder, array $options)
{
//don't need to set the $post here

$builder->add('content', TextareaType::class, [
'constraints' => [
new Assert\NotBlank(['message' => 'Your comment cannot be blank.']),
new Assert\Length([
'min' => 10,
'minMessage' => 'Your comment must be at least {{ limit }} characters long.',
]),
],
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Comment::class
//don't need the default here either
]);
}

评论实体

class Comment 
{
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Post")
*/
private $post;

//other vars

public function setPost(\App\Entity\Post $post): void
{
$this->post = $post;
}

public function getPost(): \App\Entity\Post
{
return $this->post;
}

//other functions
}

关于php - 如何将父实体传递给 Symfony 中的表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53146528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com