我得到錯誤「SQLSTATE [HY093]:無效參數號」,當我嘗試運行以下功能:SQLSTATE [HY093]:無效的參數號:無參數的約束
public function showAction(Post $post, Request $request){
$comment = new Comment();
$comment->setPost($post);
//comment->setUser($user);
$form = $this->createForm(CommentType::class, $comment); /*obiekt formularza */
$form->handleRequest($request);
if($form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush('success', 'Komentarz został pomyślnie dodany');
$this->addFlash();
return $this->redirectToRoute('post_show', array('id' =>$post->getId()));
}
return $this->render('default/show.html.twig', array(
'post' => $post,
'form' => $form->createView()
));
}
我CommentTye形式:
class CommentType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('content', \Symfony\Component\Form\Extension\Core\Type\TextareaType::class, array(
'label' => false,
'attr' => array('placeholder' => 'Treść komentarza')
))
->add('createdAt')
->add('post')
->add('user')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Comment'
));
}
/**
* @return string
*/
public function getName()
{
return 'appbundle_comment';
}
}
當我運行它,我得到
Message: An exception occurred while executing 'INSERT INTO comment (content, created_at, post_id, user_id) VALUES (?, ?, ?, ?)':
SQLSTATE [HY093]:無效參數號:沒有參數分別爲:b ound
請幫我解決這個問題
請修復代碼塊在你的問題(使用4空間縮進代碼) - 這是不可讀這樣。 – nifr
對不起,這是我的第一篇文章。 – Aga95
嘗試調用'$ em-> flush()'不帶參數。其實第一個也是唯一的參數是'$ entity',你不應該在那裏提供'成功'。 –