代码之家  ›  专栏  ›  技术社区  ›  Gaylord.P

条令返回错误与“eq”,不与“in”

  •  0
  • Gaylord.P  · 技术社区  · 6 年前

    对于symfony和district,我在“eq”子查询中有一个错误:

    没问题,没有错误:

    public function getForums()
    {
        $qb = $this->createQueryBuilder('fc');
    
        return $qb
            ->innerJoin('fc.versions', 'fcv')
            ->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->in(
                'fcvl.id',
                $this->_em->createQueryBuilder()
                    ->select('MAX(v.id)')
                    ->from(ForumCategoryVersion::class, 'v')
                    ->where('v.forumCategory = fc')
                    ->getDQL()
            ))
            ->select('fc, fcv')
            ->getQuery()
            ->getResult();
    }
    

    替换 in 通过 eq :

    public function getForums()
    {
        $qb = $this->createQueryBuilder('fc');
    
        return $qb
            ->innerJoin('fc.versions', 'fcv')
            ->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->eq(
                'fcvl.id',
                $this->_em->createQueryBuilder()
                    ->select('MAX(v.id)')
                    ->from(ForumCategoryVersion::class, 'v')
                    ->where('v.forumCategory = fc')
                    ->getDQL()
            ))
            ->select('fc, fcv')
            ->getQuery()
            ->getResult();
    }
    

    我有这个错误:

    [语法错误]第0行第208列:错误:应为文本,得到“select”

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jannes Botis    6 年前

    你需要使用 括号 () 对于 子查询 :

    ->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->eq(
            'fcvl.id',
            '(' . $this->_em->createQueryBuilder()
                ->select('MAX(v.id)')
                ->from(ForumCategoryVersion::class, 'v')
                ->where('v.forumCategory = fc')
                ->getDQL() . ')'
        ))
    

    工具书类