Doctrine DQL子查询给出零结果

时间:2015-02-12 15:23:53

标签: php mysql symfony doctrine-orm

我有三个相关实体:Mysql服务器上的Group,GroupDefault,GroupDefaultTranslation。

  • Group实体存储用户的组。
  • GroupDefault实体为任何用户定义默认组。
  • GroupDefaultTranslation实体存储默认组的翻译。

实体之间的关系是类型:   - Group和GroupDefault之间的ManyToOne   - OneToManu ent GroupsDefault和GroupDefaultTranslation

我的目标是在他的语言参数中显示具有相应翻译的用户组列表。

这是我生成的DQL查询的QueryBuilder:

public function findByOwnerWithLanguage($languageCode, $ownerId)
{
        $qb = $this->createQueryBuilder('g')
            ->select('g.name')
            ->addSelect('gdt.content AS name')
            ->join('g.parent', 'gd')
            ->join('gd.translations', 'gdt')
            ->where('g.owner = :owner');

            //création de l'expression AND
            $orModule = $qb->expr()->andX();
            $orModule->add($qb->expr()->eq('g.parent', '0'));
            $orModule->add($qb->expr()->eq('gdt.locale', ':language'));
            $orModule->add($qb->expr()->eq('g.owner', ':owner'));


         //Ajout de l'expression à la requête
         $qb->orWhere($orModule)
            ->setParameter('language', $languageCode, \PDO::PARAM_STR)
            ->setParameter('owner', $ownerId)
            ->orderBy('g.isMyLightbox', 'DESC')
            ->getQuery()
            ->getResult();
    return $qb;

    // DQL Query :

    // SELECT g.name, gdt.content AS name
    // FROM Horyou\Bundle\CoreBundle\Entity\Group g
    // INNER JOIN g.parent gd
    // INNER JOIN gd.translations gdt
    // WHERE g.owner = :owner
    // OR (g.parent = 0 AND gdt.locale = :language AND g.owner = :owner)
    // ORDER BY g.isMyLightbox DESC

}

这是对象var_dump的输出:

object(stdClass)[1368]
  public '__CLASS__' => string 'Doctrine\ORM\QueryBuilder' (length=25)
  public '_em' => 
    object(stdClass)[1362]
      public '__CLASS__' => string 'Doctrine\ORM\EntityManager' (length=26)
      public 'config' => string 'Doctrine\ORM\Configuration' (length=26)
      public 'conn' => string 'Doctrine\DBAL\Connection' (length=24)
      public 'metadataFactory' => string 'Doctrine\ORM\Mapping\ClassMetadataFactory' (length=41)
      public 'unitOfWork' => string 'Doctrine\ORM\UnitOfWork' (length=23)
      public 'eventManager' => string 'Symfony\Bridge\Doctrine\ContainerAwareEventManager' (length=50)
      public 'proxyFactory' => string 'Doctrine\ORM\Proxy\ProxyFactory' (length=31)
      public 'repositoryFactory' => string 'Doctrine\ORM\Repository\DefaultRepositoryFactory' (length=48)
      public 'expressionBuilder' => string 'Doctrine\ORM\Query\Expr' (length=23)
      public 'closed' => boolean false
      public 'filterCollection' => string 'Doctrine\ORM\Query\FilterCollection' (length=35)
  public '_dqlParts' => 
    array (size=9)
      'distinct' => boolean false
      'select' => string 'Array(2)' (length=8)
      'from' => string 'Array(1)' (length=8)
      'join' => string 'Array(1)' (length=8)
      'set' => string 'Array(0)' (length=8)
      'where' => string 'Doctrine\ORM\Query\Expr\Orx' (length=27)
      'groupBy' => string 'Array(0)' (length=8)
      'having' => null
      'orderBy' => string 'Array(1)' (length=8)
  public '_type' => int 0
  public '_state' => int 1
  public '_dql' => string 'SELECT g.name, gdt.content AS name FROM Horyou\Bundle\CoreBundle\Entity\Group g INNER JOIN g.parent gd INNER JOIN gd.translations gdt WHERE g.owner = :owner OR (g.parent = 0 AND gdt.locale = :language AND g.owner = :owner) ORDER BY g.isMyLightbox DESC' (length=251)
  public 'parameters' => 
    array (size=2)
      0 => string 'Doctrine\ORM\Query\Parameter' (length=28)
      1 => string 'Doctrine\ORM\Query\Parameter' (length=28)
  public '_firstResult' => null
  public '_maxResults' => null
  public 'joinRootAliases' => 
    array (size=2)
      'gd' => string 'g' (length=1)
      'gdt' => string 'g' (length=1)

我的问题是结果为零。 有谁看到我的QueryBuilder中的错误是什么?

1 个答案:

答案 0 :(得分:0)

实际上它不是结果 - 它只是一个查询构建器对象的转储。我想你必须做这样的事情:

...
$qb->orWhere($orModule)
        ->setParameter('language', $languageCode, \PDO::PARAM_STR)
        ->setParameter('owner', $ownerId)
        ->orderBy('g.isMyLightbox', 'DESC');
$result = $qb->getQuery()->getResult();

return $result;