Symfony2,doctrine自定义查询

时间:2013-02-03 18:04:43

标签: symfony doctrine

  

可能重复:
  Symfony2 & Doctrine: Create custom SQL-Query

我在我的symfony2项目上试过了:

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
        'SELECT v.voiture,o.offre,m.marque
         FROM FrontOfficeBundle:Voiture v
         INNER JOIN FrontOfficeBundle:OffreSpecial o on o.voiture_id = v.id
         INNER JOIN FrontOfficeBundle:Marque m on m.id = v.marque_id'
    );
$result = $query->getResult();

并得到那个错误:

[Syntax Error] line 0, col 122: Error: Expected Doctrine\ORM\Query\Lexer::T_WITH, got 'on'

甚至SQL查询都是正确的!请帮忙

1 个答案:

答案 0 :(得分:1)

ON您不需要INNER JOIN选项。由于您在查询中指定了类FrontOfficeBundle:Voiture,因此Doctrine查询语言已经知道您的关联的映射。

$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
        'SELECT v.voiture,o.offre,m.marque
         FROM FrontOfficeBundle:Voiture v
         INNER JOIN v.offreSpecial o 
         INNER JOIN v.marque m'
    );
$result = $query->getResult();