右加入学说



我试图在Symfony中正确加入。我尝试了此处描述的学说2-外部加入查询和Symfony-使用Outs of Outs与学说ORM。

$query = $em->getRepository('AppBundle:raports')->createQueryBuilder('r')
            ->select('r')
            ->leftJoin('r.requestRaports rr WITH rr.formId = :formId', false)
            ->setParameter('formId', $requestId->getFormId())
            ->getQuery();

它给出

 SELECT 
  r0_.id AS id_0, 
  r0_.adminComment AS adminComment_1, 
  r0_.addDate AS addDate_2, 
  r0_.submitDate AS submitDate_3, 
  r0_.statusId AS statusId_4, 
  r0_.userId AS userId_5, 
  r0_.requestId AS requestId_6, 
  r0_.requestRaports AS requestRaports_7 
FROM 
  raports r0_ 
  LEFT JOIN request_raports r1_ ON r0_.requestRaports = r1_.id 
  AND (r1_.formId = ?)

当我尝试

$query = $em->getRepository('AppBundle:raports')->createQueryBuilder('r')
            ->select('r')
            ->join('r.requestRaports rr WITH rr.formId = :formId', false)
            ->setParameter('formId', $requestId->getFormId())
            ->getQuery();

看起来像

SELECT 
  r0_.id AS id_0, 
  r0_.adminComment AS adminComment_1, 
  r0_.addDate AS addDate_2, 
  r0_.submitDate AS submitDate_3, 
  r0_.statusId AS statusId_4, 
  r0_.userId AS userId_5, 
  r0_.requestId AS requestId_6, 
  r0_.requestRaports AS requestRaports_7 
FROM 
  raports r0_ 
  INNER JOIN request_raports r1_ ON r0_.requestRaports = r1_.id 
  AND (r1_.formId = ?)

,但我想要像

这样的查询

raports r选择 *右加入request_raports rr on on r。 requestRaports = rr.id

如何使正确加入Doctrine2?

SELECT * FROM request_raports rr LEFT JOIN raports r ON r.requestRaports = rr.id

或者您可以创建自己的"正确加入"。如果您在学说中查看左键定义,就像:

leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
    $parentAlias = substr($join, 0, strpos($join, '.'));
    $rootAlias = $this->findRootAlias($alias, $parentAlias);
    $join = new ExprJoin(
        ExprJoin::LEFT_JOIN, $join, $alias, $conditionType, $condition, $indexBy
    );
    return $this->add('join', array($rootAlias => $join), true);
}

所以,看起来像这样:

$qb = $this->createQueryBuilder('b');
$rightJoin = new ExprJoin('RIGHT', 'r.requestRaports', 'rr', ExprJoin::WITH, 'rr.formId = :formId');
$qb
    ->select('r')
    ->add('join', ['r' => $rightJoin], true)
    ...

我尚未测试过,不知道它是最好的方法...

相关内容

  • 没有找到相关文章

最新更新