2015-10-23 68 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