Nat*_*ael 8 php symfony symfony4
假设我有两个实体:a post和a comment.每个都post可以有很多comments.现在,假设我有一个评论表.它应该接受用户输入并将其存储在数据库中.
简单的东西.至少,它应该是,但我不能让它工作.
创建评论(孩子)时如何引用帖子(父母)?我尝试手动将post_id注释表单作为隐藏字段传递,但收到错误抱怨帖子ID是一个字符串.
Expected argument of type "App\Entity\Post or null", "string" given.
Run Code Online (Sandbox Code Playgroud)
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,
]);
}
Run Code Online (Sandbox Code Playgroud)
PostController.php(这是评论表格出现的地方)
// Generate the comment form.
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment, [
'action' => $this->generateUrl('new_comment'),
'post_id' => $post_id,
]);
Run Code Online (Sandbox Code Playgroud)
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');
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助!
这段代码对我有用:
CommentController.php
正如上面 flint 所建议的,您只需要传递实际的 Post 实体,而不仅仅是 id。然后,如果出现此错误,"Unable to guess how to get a Doctrine instance from the request information for parameter "post"这是因为您需要在new_comment路由的路径中添加post slug 。ParamConverter 是隐式调用的,它需要这个 slug {post} ,其名称与您在函数中用于post参数的名称相同。
/**
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* @Route("/comment/new/{post}", name="new_comment")
*/
public function new(Request $request, Post $post)
{
$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');
}
Run Code Online (Sandbox Code Playgroud)
PostController.php
/**
* @Route("/post/{id}", name="get_post")
*/
public function getPostAction(Post $post)
{
// Generate the comment form.
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment, [
'action' => $this->generateUrl('new_comment', ['post' => $post->getId()]),
]);
return $this->render('listeArticles.html.twig', [
'form' => $form->createView()
]);
}
Run Code Online (Sandbox Code Playgroud)
评论类型.php
class CommentType extends AbstractType
{
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.',
]),
],
])
->add('submit', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Comment::class
]);
}
}
Run Code Online (Sandbox Code Playgroud)
这样您就不需要删除两个表之间的 Doctrine 关系并手动设置 ID。
| 归档时间: |
|
| 查看次数: |
303 次 |
| 最近记录: |