2015-10-23 31 views
-2

我想在DQL请求中添加自定义方法时出现错误。 错误:定制存储库(REQUEST DQL)

Undefined method 'getAll'. The method name must start with either findBy or findOneBy!

我的控制器:(SheetController.php)

<?php 
namespace Test\FrontBundle\Controller; 

use Doctrine\ORM\EntityNotFoundException; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\JsonResponse; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\Response; 
use Test\FrontBundle\Entity\Sheet; 

class SheetController extends Controller 
{ 
    public function sheetListAction(Request $request) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $repository = $em->getRepository('TestFrontBundle:Sheet'); 

     $sheets = $repository->getAll(); 
     var_dump($sheets); 
     return $this->render('TestFrontBundle:Sheet:sheetList.html.twig'); 
    } 
    public function sheetAction($id, Request $request) 
    { 
     $repository = $this->getDoctrine()->getManager()->getRepository('TestFrontBundle:Sheet'); 
     $sheet = $repository->find($id); 
     if(!$sheet) 
     { 
      throw new EntityNotFoundException(); 
     } 
     return $this->render('TestFrontBundle:Sheet:sheet.html.twig', array('sheet' => $sheet)); 
    } 
} 
?> 

我的仓库:(SheetRepository.php)

<?php 

namespace Test\FrontBundle\Entity; 

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

class SheetRepository extends \Doctrine\ORM\EntityRepository 
{ 
    public function getAll() 
    { 
     $qb = $this->createQueryBuilder('s'); 

     $query = $qb; 

     $result = $query->getQuery()->execute(); 

     return $result; 
    } 
} 

请,你能不能帮我? :)

+0

您需要将存储库的位置添加到您的实体映射。 '@ORM \实体(repositoryClass = “...”)'。检查你的配置。 – Artamiel

+0

嗨,是的。在Entity/Sheet.php上我有('/ ** * Sheet * * @ORM \ Table() * @ORM \ Entity(repositoryClass =“Test \ FrontBundle \ Entity \ SheetRepository”) * /)' – whitesmoke

+0

谢谢Artamiel!是的,我忘了在我的资源库中放入“'@ORM \ Entity(repositoryClass =”Test \ FrontBundle \ Entity \ SheetRepository'“)!非常感谢! – whitesmoke

回答

0

为什么你不使用本地原则查询findAll()为此?

public function sheetListAction(Request $request) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $repository = $em->getRepository('TestFrontBundle:Sheet'); 

    $sheets = $repository->findAll(); 
    /***/ 
} 

编辑 - 随着分类储存:

class SheetRepository extends \Doctrine\ORM\EntityRepository 
{ 
    public function getAll() 
    { 
     return $this->createQueryBuilder('s') 
      ->select('s') 
      ->getQuery() 
      ->getResult() 
     ; 
    } 
} 

而在你的控制器:更换findAll()通过getAll()

+0

因为我想”添加“我的第一个自定义方法^^'我在学习Symfony2,这是我第一次尝试“添加”一个方法:s但是我知道“findAll”具有相同的影响,我想尝试DQL。 – whitesmoke

+0

@scoolnico - 你不需要把所有的逻辑在'SheetRepository'的'getAll()'中,你可以在'getAll()'方法中调用'$ this-> findAll();' – BentCoder

+0

你真的看过我的答案吗?... – scoolnico