2013-02-05 51 views
3

這裏查詢通過主義是摘錄到我的代碼時,我嘗試查詢像這樣無法識別的領域:USER_NAME,而使用ZF2

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; 

我得到以下錯誤

無法識別的領域: USER_NAME

這些都是我的包括

使用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 
} 

?> 
+0

你可以發佈你的'Album \ Entity \ Album'類嗎? –

+0

似乎字段'user_name'不存在。您是否使用屬性名稱進行查詢?數據庫字段名稱與Doctrine不相關,只有屬性的名稱是。 –

+0

是的你都指出正確我已經更新它現在你可以弄明白了嗎? –

回答

5

您要查詢的錯誤的領域。該字段在您的實體類中被命名爲_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標準。方法和變量名中的下劃線現在不鼓勵。