aga
aga

Reputation: 3903

SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

I'm getting the error "SQLSTATE[HY093]: Invalid parameter number" when I try to run the below function:

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()
    ));
}

My CommentTye form:

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';
}
}

When I run it, I get

Message: An exception occurred while executing 'INSERT INTO comment (content, created_at, post_id, user_id) VALUES (?, ?, ?, ?)':

SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

Please help me fix this

Upvotes: 0

Views: 2117

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52513

You have mixed up flushing your entity-manager and adding a flash message.

Instead of:

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

You want:

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

Upvotes: 2

Related Questions