Unrecognized field: username, when queried through Doctrine using ZF2

here is a snippet of my code when i try to query it like this

   if ($request->isPost()) {
        $form->setData($request->getPost());
        if ($form->isValid()) {

            //check authentication...
            $this->getAuthService()->getAdapter()
                    ->setIdentity($request->getPost('username'))
                    ->setCredential($request->getPost('password'));

            $username = $request->getPost('username');
            $password = $request->getPost('password');
            $result = $this->getAuthService()->authenticate();

            $criteria = array("user_name" => $username,);
           $results= $this->getEntityManager()->getRepository('Subject\Entity\User')->findBy($criteria);
           print_r($results);
           exit;

      

i am getting the following error

Unrecognized field: username

These are my included

Use Doctrine \ ORM \ EntityManager, Album \ Entity \ Album;

Edit: this is my Subject \ Entity \ User object

 <?php

namespace Subject\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

/**
 * @ORM\Entity

* @ORM\Table(name="users")

* @property string $username

* @property string $password

* @property int $id

 */
class User implements InputFilterAwareInterface {

protected $_username;
protected $_password;

 /**
 * @ORM\OneToMany(targetEntity="Subject\Entity\Subject", mappedBy="user")
 * @var Collection
 */
private $subjects;

/** @ORM\Id() @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") @var int */
protected $_id;

public function __get($property) {

    return $this->$property;
}

public function __set($property, $value) {

    $this->$property = $value;
}

//Getters and setters

/** @return Collection */
public function getSubjects() {
    return $this->subjects;
}

/** @param Comment $comment */
public function addSubject(Subject $subjects) {
    $this->subjects->add($subjects);
    $subjects->setUser($this);
}


 public function __construct($subjects) {
    //Initializing collection. Doctrine recognizes Collections, not arrays!
    $this->subjects = new ArrayCollection();

}
public function getArrayCopy() {

    return get_object_vars($this);
}

public function populate($data = array()) {

    $this->_id = $data['id'];

    $this->_username = $data['username'];

    $this->_password = $data['password'];
}

public function setInputFilter(InputFilterInterface $inputFilter) {

    throw new \Exception("Not used");
}

public function getInputFilter() {

    if (!$this->inputFilter) {
        $inputFilter = new InputFilter();
        $factory = new InputFactory();
        $inputFilter->add($factory->createInput(array(
                    'name' => 'id',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                )));
        $inputFilter->add($factory->createInput(array(
                    'name' => 'username',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 100,
                            ),
                        ),
                    ),
                )));



        $inputFilter->add($factory->createInput(array(
                    'name' => 'password',
                    'required' => true,
                    'filters' => array(
                        array('name' => 'StripTags'),
                        array('name' => 'StringTrim'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'StringLength',
                            'options' => array(
                                'encoding' => 'UTF-8',
                                'min' => 1,
                                'max' => 100,
                            ),
                        ),
                    ),
                )));



        $this->inputFilter = $inputFilter;
    }



    return $this->inputFilter;
}

//put your code here
}

?>

      

+3


source to share


1 answer


You are asking for the wrong field. The field is named _username

in the entity class. Also check the annotations _username

and _password

it seems not, so they won't be created as database fields.

If you have configured your object correctly and all fields are in the database, you just need to query for your property _username

:

 if ($request->isPost()) {
     $form->setData($request->getPost());
     $repo = $this->getEntityManager()->getRepository('Subject\Entity\User');
     if ($form->isValid()) {
         // snip ...
         $criteria = array("_username" => $username,);
         $results= $repo->findBy($criteria);
         print_r($results);
         exit;
    }
}

      



The user should look something like this:

class User implements InputFilterAwareInterface {
    /**
     * @ORM\Column(name="username", type="string", length=64, unique=true)
     */
    protected $_username;
    /**
     * @ORM\Column(name="password", type="string", length=64)
     */
    protected $_password;

    // snip ...
}

      

You can take a look at PSR-2 standards . Underscores on method and variable names are discouraged yet

+5


source







All Articles