4

我正在尝试将此 SQL 转换为 DQL 或任何查询构建器变体的样子。

select *
  from project_release r
 where (select s.title as status_name
          from release_status_log l
          left join release_status s
            on l.release_status_id = s.id
         where l.release_id = r.id
         order by l.created_at desc
         limit 1
       ) not in ('Complete', 'Closed')
;

Release实体的存储库类内部,我试过这个

return $this->getEntityManager()->createQuery("
    select r.*
      from MyBundle:Release r
     where (select s.title
              from MyBundle:ReleaseStatusLog l
              join l.status s
             where l.release = r
             order by l.createdAt desc
             limit 1
           ) IN ('Complete','Closed')
     order by r.release_date ASC
     limit 10
")->getArrayResult();

这给出了错误

[语法错误] 第 0 行,第 265 列:错误:预期的 Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS,得到“限制”

这是指limit 1子查询中的。

所以我尝试了这个

return $this
  ->createQueryBuilder('r')
  ->select('r.*')
  ->where("(select s.title
              from MyBundle:ReleaseStatusLog l
              join l.status s
             where l.release = r
             order by l.created_at desc
             limit 1
           ) $inClause ('Complete', 'Closed')
  ")
  ->setMaxResults( $limit )
  ->orderBy('release_date', 'ASC')
  ->getQuery()
  ->getArrayResult()
;

这给出了同样的错误。如何在父查询中执行限制为每行 1 行的子查询?

  • Symfony 2.0.15
  • 教义 2.1.7
  • PHP 5.3.3
  • MySQL 5.1.52
4

2 回答 2

2

我现在有一个解决方案。我最终使用了来自问题实体的结果集映射的本机查询系统。

这不是一个很好的解决方案,但它有效,直到我看到另一个解决方案,它是这种 WHERE 子句类型的唯一选择。

这是我的finder方法现在的样子

/**
 * Finds Releases by their current status
 *
 * @param array $statuses      White-list of status names
 * @param boolean $blackList   Treat $statuses as a black-list
 * @param integer $limit       Limit the number of results returned
 * @param string $order        Sort order, ASC or DESC
 *
 * @throws \InvalidArgumentException
 *
 * @return array <Release>
 */
public function findByCurrentStatus( array $statuses, $blackList=false, $limit=null, $order='ASC' )
{
  if ( empty( $statuses ) )
  {
    throw new \InvalidArgumentException( "Must provide at least one status" );
  }
  $inClause = $blackList ? 'not in' : 'in';

  $rsm = new ResultSetMappingBuilder($this->getEntityManager());
  $rsm->addRootEntityFromClassMetadata('MyBundle:Release', 'r');

  $SQL = "
    select *
      from project_release r
     where (select s.title as status_name
              from release_status_log l
              left join release_status s
                on l.release_status_id = s.id
             where l.release_id = r.id
             order by l.created_at desc
             limit 1
           ) $inClause ('" . implode( "','", $statuses ) . "')
    order by r.release_date $order
  ";

  if ( $limit )
  {
    $SQL .= " limit $limit";
  }

  return $this
    ->getEntityManager()
    ->createNativeQuery( $SQL, $rsm )
    ->getResult()
  ;
}

我有点讨厌回到将查询构建为字符串,但是哦,好吧。哦,对于老鹰眼来说,$statuses不是来自用户数据,所以这里没有 SQL 注入漏洞;)

于 2013-06-12T16:37:53.347 回答
0

除了您的 Native SQL 解决方案之外,您还可以在单​​个存储库方法中使用 DQL 创建两个查询。

可能需要进行一些调整,但您可以尝试以下操作:

public function findCompletedReleases()
{
    $em = $this->getEntityManager();

    $dqlSubQuery = <<<SQL
SELECT
  s.title status_name
FROM
  Acme\MyBundle\Entity\ReleaseStatus s,
  Acme\MyBundle\Entity\ReleaseStatusLog l,
  Acme\MyBundle\Entity\Release r
WHERE
  l.release = r.id AND
  l.status  = s.id
ORDER BY l.createdAt DESC
SQL;

    $statusName = $em->createQuery($dqlSubQuery)
        ->setMaxResults(1)
        ->getSingleScalarResult();

    $dql = <<<SQL
SELECT
  r
FROM
  Acme\MyBundle\Entity\Release r
WHERE
  :status_name IN ('Complete','Closed')
ORDER BY r.release_date ASC
SQL;

    $q = $em->createQuery($dql)
        ->setParameters(array('status_name' => $statusName))
        ->setMaxResults(10);

    return $q->getArrayResult();
}
于 2013-06-12T18:44:31.217 回答