我想在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;
}
}
請,你能不能幫我? :)
您需要將存儲庫的位置添加到您的實體映射。 '@ORM \實體(repositoryClass = 「...」)'。檢查你的配置。 – Artamiel
嗨,是的。在Entity/Sheet.php上我有('/ ** * Sheet * * @ORM \ Table() * @ORM \ Entity(repositoryClass =「Test \ FrontBundle \ Entity \ SheetRepository」) * /)' – whitesmoke
謝謝Artamiel!是的,我忘了在我的資源庫中放入「'@ORM \ Entity(repositoryClass =」Test \ FrontBundle \ Entity \ SheetRepository'「)!非常感謝! – whitesmoke