我需要將原始查詢作爲字符串獲取,如下所示。如何從控制器獲取來自EntityRepository的原始查詢
$查詢= '選擇P檔FROM GabrielUploadBundle:圖像P WHERE p.upvotes> X ORDER BY p.createdAt ASC';
我的自定義「findAllNewestByVotes」方法包含查詢。
class ImageRepository extends EntityRepository
{
public function findAllNewestByVotes($maxvotes)
{
return $this->getEntityManager()
->createQuery(
'SELECT p FROM GabrielUploadBundle:Image p WHERE p.upvotes > '.$maxvotes.' ORDER BY p.createdAt ASC')
->getResult();
}
}
/**
* @Route("/world/front",name="world-front")
* @Template()
*/
public function indexAction()
{
$images = $this->get('doctrine')->getRepository('GabrielUploadBundle:Image')->findAllNewestByVotes(50);
ladybug_dump($images);
return $this->render('GabrielLayoutBundle:Worldpage:index.html.twig',array('images'=>$images));
}
我需要的是像 $圖像 - > getRawQuery()//返回查詢作爲字符串
解決方案(參考最好的答案)
/**
* ImageRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ImageRepository extends EntityRepository
{
public function findAllNewestByVotes($maxvotes)
{
return $this->getEntityManager()
->createQuery(
'SELECT p FROM GabrielUploadBundle:Image p WHERE p.upvotes > '.$maxvotes.' ORDER BY p.createdAt ASC');
}
}
> create image repository
$images = $this->get('doctrine')->getRepository('GabrielUploadBundle:Image')->findAllNewestByVotes(50);
return the raw query as string like this
$images->getDQL()
return objects like this
$images->getResult();
查詢作爲字符串是相當無用的,除非你正在考慮追加到它作爲一個字符串,它是醜陋的,因爲教義有這麼多容易的動態構建SQL查詢使用功能。 –
如果它沒用,他們就不會創建getDQL()方法 (查看答案) – user3531149
對我們來說這是沒用的 - 圖書館的受益者/用戶 - 它對於較低級別的工作很有用。 –