在Symfony2中使用Doctrine定义自定义存储库类

时间:2013-09-02 14:08:57

标签: class symfony doctrine repository

我在symfony中定义自定义存储库类时遇到问题,以便使用一些自定义查询(方法User :: checkUser())。

这是存储库类代码

<?php
namespace TDDumb\FEBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityRepository;
/**
 *
 *  @ORM\Entity(repositoryClass="TDDumb\FEBundle\Entity\UserRepository")
 *  @ORM\Table(name="user")
 *
 */

class User extends EntityRepository{
    public function __construct(){
    }
    /**
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     *
     * @ORM\Column(type="string", length=16)
     */
    protected $userID;
    /**
     *
     * @ORM\Column(type="string", length=16)
     */
    protected $password;


[GETTER AND SETTER...]

    public function checkUser(){
        return $this->getEntityManager()->createQuery("SELECT u FROM TDDumbFEBundle:user u WHERE u.userID = '{$this->userID}' AND u.password = '{$this->password}'")->getResult();
    }
}

这是我的控制器代码:

public function loginAction(Request $request){
        $user = new User();

        $form = $this->createFormBuilder($user)
        ->add('userID', 'text', array('label' => 'UserID'))
        ->add('password', 'password', array('label' => 'Password'))
        ->add('ricorda', 'checkbox', array(
                "required" => false,
                "mapped" => false
        ))
        ->add('login', 'submit')->getForm();

        $form->handleRequest($request);

        if($form->isValid()){
            //$dbUser = $this->getDoctrine()->getRepository('TDDumbFEBundle:User')->findOneByUserID($user->getUserID());
            $em = $this->getDoctrine()->getManager();
            $res = $em->getRepository('TDDumbFEBundle:User')->checkUser();
            Debug::dump($res);
        }
        return $this->render('TDDumbFEBundle:Default:auth.html.twig', array('form' => $form->createView()));
    }

我无法理解为什么我总是会收到这个错误:

FatalErrorException: Error: Class 'TDDumb\FEBundle\Entity\UserRepository' not found in /var/www/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php line 689

哪个指向类文件中的映射定义:

@ORM\Entity(repositoryClass="TDDumb\FEBundle\Entity\UserRepository")

这是正确的,我想

0 个答案:

没有答案