php - 使用 ZF2 通过 Doctrine 查询时无法识别的字段 : user_name,

标签 php doctrine zend-framework2

这是我的代码片段,当我尝试这样查询时

   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;

我收到以下错误

Unrecognized field: user_name

这些是我的内容

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

编辑:这是我的主题\实体\用户文件

 <?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
}

?>

最佳答案

您正在查询错误的字段。该字段在您的实体类中被命名为 _username。还要检查你的注释,_username_password 似乎没有任何注释,因此它们不会被创建为数据库字段。

如果您正确设置了您的实体并且所有字段都在数据库中,您只需要查询您的 _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;
    }
}

您的用户实体应该类似于:

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 ...
}

你可以看看 PSR-2标准。现在不鼓励在方法和变量名称中使用下划线。

关于php - 使用 ZF2 通过 Doctrine 查询时无法识别的字段 : user_name,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14709039/

相关文章:

php - Symfony Doctrine EntityManager 没有正确刷新

php - 如何在 Zend Framework 2 中向表单添加错误消息?

php - 无法通过 ZF2 中的 Pdo_Mysql 通过 Google App Engine 连接到 Google Cloud SQL

javascript - 使用php动态设置输入值

javascript - 拖放后对表中的行重新编号

mysql - Doctrine:两个数据库中两个实体之间的关系

php - 鉴别器图中缺少的实体

zend-framework2 - Zend/ZF2/TableGateway mysql_insert_id 替换?

php - 基于一组可变的比较点检索 MySQL 记录

php - 如何在 PHP 中设置时间/日期的格式?