SQLSTATE [HY093]:参数号无效:没有绑定参数

时间:2016-10-30 15:06:57

标签: php symfony

我收到错误" 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()
    ));
}

我的评论表格形式:

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]:参数号无效:没有绑定参数

请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

您已混淆刷新您的实体经理并添加 flash 消息。

而不是:

$em->flush('success', 'Komentarz został pomyślnie dodany');
$this->addFlash();

你想:

$em->flush();
$this->addFlash('success');
$this->addFlash('Komentarz został pomyślnie dodany');