在symfony2中的非对象上调用成员函数createQuery()

时间:2013-05-14 09:34:10

标签: php symfony doctrine-orm

但我在注册实体中添加了@ORM\Entity(repositoryClass="Repair\StoreBundle\Entity\registrationRepository"),但我在createQuery()附近仍然收到错误;

我的registrationRepository.php是

use Doctrine\ORM\EntityRepository;

/**
 * registrationRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */

class registrationRepository extends EntityRepository
{

    function __construct() {
    }

    public function auth($name,$password)
    {
        $em = $this->getEntityManager();

        $result = $em->createQuery("SELECT r.username,r.password FROM RepairStoreBundle:registration r where r.username='".$name."' and r.password='".$password."'")->getResult();
        return  $query;

    }   
}

我的控制器正在以这种方式呼叫

$test = new registrationRepository();
$result = $test->auth($name);

1 个答案:

答案 0 :(得分:4)

在Controller中创建new registrationRepository(实例化)manualy是这样做的错误方法。相反,你必须使用doctrine getRepository方法:

$em = $this->getDoctrine()->getManager();
$registrationRepo = $em->getRepository('RepairStoreBundle:registration');
$result = $registrationRepo ->auth($name);

阅读the docs

更新

此外,您应该从存储库类中删除__construct方法。