Zend Framework 2 - Doctrine 2 Error message

2019-09-06 14:54发布

问题:

I have been trying to add more flexibilities to the Album Module from Zend Framework 2. In that process I have been trying to set a validator for one of the form fields especially for the album name which in my case the column name in my database is title.

I have been following the validation part from one of the previous answers to my post, which can be found here

I have been using that class in my albumcontroller class in this fashion:

<?php

namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Entity\Album\Album;
use Album\Form\AlbumForm;
use Album\Model\Album\AlbumExists;
use Doctrine\ORM\EntityManager;

class AlbumController
extends AbstractActionController
{
 public function addAction()
     {
        $form = new AlbumForm();
        $form->get('submit')->setAttribute('value', 'Add');

        $query = "SELECT a.title FROM Album\Entity\Album\Album a";
        $albumExists = new AlbumExists($this->getEntityManager(), $query, 'title');

        $request = $this->getRequest();
        if ($request->isPost())
        {
            $album = new Album();
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());
            $title = $this->getRequest()->getPost('title');

            if ($form->isValid() && $albumExists->isValid($title))
            {
                 $album->populate($form->getData());
                 $this->getEntityManager()->persist($album);
                 $this->getEntityManager()->flush();

                return $this->redirect()->toRoute('album');
            }
        }
        return array('form' => $form);
    }

When I enter a album name/title which is already in the database it throws an error in this fashion:

An error occurred during execution; please try again later.  
Additional information:
Doctrine\ORM\Query\QueryException  

File:
C:\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php:69

Message:
Invalid parameter number: number of bound variables does not match number of tokens.

Any idea where Im making a mistake?

回答1:

In case you're using "my" class and haven't modified that part, you're missing the WHERE condition in your query.

In the class, a parameter :value is bound, so you have to use this parameter in your query (e.g. WHERE a.title = :value).