Doctrine2 - 没有关系的子查询连接

时间:2013-01-16 00:14:55

标签: symfony doctrine-orm dql query-builder

好的,这就是问题所在。

我有一个名为HelpDocuments的实体和一个名为LogEntry的实体。

帮助文档可以被用户解雇。发生这种情况时,我创建一个具有以下属性的LogEntry:

  • 事件 - 例如:helpDocument.dismiss
  • entity_id - 例如:11
  • entityDiscriminator - 例如:HelpDocument

在HelpDocument和LogEntry之间没有创建任何关系,因为我正在实现自己的鉴别器逻辑。

所以我想要实现的是查询尚未被解雇的所有HelpDocuments。我可以用sql执行,左外部子查询连接如下:

SELECT HelpDocument.*, temp.*
FROM HelpDocument
LEFT OUTER JOIN(
    SELECT LogEntry.entity_id
    FROM LogEntry
    WHERE LogEntry.entityDiscriminator = 'HelpDocument'
    AND LogEntry.event = 'helpDocument.dismiss'
    AND LogEntry.entity_id = 11
) as temp ON HelpDocument.id = temp.entity_id
WHERE temp.entity_id IS NULL;

我的问题是,如果没有定义关系,我该如何将其转换为DQL?


更新了解决方案:

因此,解决方案是不使用LEFT OUTER JOIN,因为它们在Doctrine2中不存在/有意义。最后我不得不做一个子查询加入:

/**
 * Filter by User Dismissed
 *
 * @param $qb
 * @param $route
 * @return mixed
 */
public function filterQueryByUserDismissed(QueryBuilder $qb, $args)
{
    $args = array_merge(array(
        "user" => null,
        "dismissed" => false
    ), $args);

    /** @var $dismissedQB QueryBuilder */
    $dismissedQB = $this->_em->createQueryBuilder();

    /*
    This line is important. We select an alternative attribute rather than
    letting Doctrine select le.id
    */
    $dismissedQB->select('le.entityId')
                ->from('\Mlf\AppBundle\Entity\UserEntityEventLog', 'le')
                ->where('le.entityDiscriminator = :entityDiscriminator')
                ->andWhere('le.event = :event')
                ->andWhere('le.user = :userId');

    $function = (true === $args['dismissed']) ? "in" : "notIn";
    $expr = $qb->expr()->$function($this->classAlias.'.id', $dismissedQB->getDQL());

    /** @var $qb QueryBuilder */
    $qb->andWhere($expr)
       ->setParameter("entityDiscriminator", HelpDocument::getDiscriminator())
       ->setParameter("event", HelpDocumentEvents::HELPDOCUMENT_DISMISS)
       ->setParameter("userId", $args["user"]);

//  exit($result = $qb->getQuery()->getSQL());

    return $qb;
}

此DQL查询产生以下SQL:

SELECT h0_.id AS id0
FROM HelpDocument h0_ 
WHERE (
    h0_.id NOT IN (
        SELECT l1_.entity_id 
        FROM LogEntry l1_ 
        WHERE l1_.entityDiscriminator = 'helpDocument' 
        AND l1_.event = 'helpDocument.dismiss' 
        AND l1_.user_id = 1
    )
)

耶!

1 个答案:

答案 0 :(得分:0)

我看到了你的解决方案,我有一个小的改变,这将是一个巨大的性能改进。特别是如果你有超过几千行。

public function filterQueryByUserDismissed(QueryBuilder $qb, $args)
{
  $args = array_merge(array(
    "user" => null,
    "dismissed" => false
  ), $args);

  /** @var $dismissedQB QueryBuilder */
  $dismissedQB = $this->_em->createQueryBuilder();

  /*
  This line is important. We select an alternative attribute rather than
  letting Doctrine select le.id
  */
  $dismissedQB->select('le.entityId')
            ->from('\Mlf\AppBundle\Entity\UserEntityEventLog', 'le')
            ->where('le.entityDiscriminator = :entityDiscriminator')
            ->andWhere('le.event = :event')
            ->andWhere('le.user = :userId');

  // ---- My changes below
  // Get an array with the ids
  $dismissedIdsMap = $dismissedQB->getQuery()->getResults();
  $dismissedIds = array_map(
        function($a){
            return $a['entityId'];
        },
        $dismissedIdsMap);

  $function = (true === $args['dismissed']) ? "in" : "notIn";
  $expr = $qb->expr()->$function($this->classAlias.'.id', $dismissedIds);
  // ---- My changes above

  /** @var $qb QueryBuilder */
  $qb->andWhere($expr)
   ->setParameter("entityDiscriminator", HelpDocument::getDiscriminator())
   ->setParameter("event", HelpDocumentEvents::HELPDOCUMENT_DISMISS)
   ->setParameter("userId", $args["user"]);

  //  exit($result = $qb->getQuery()->getSQL());

  return $qb;
}

上面的代码使用了两个查询。如果您使用一个查询,MySQL将从您的子查询创建一个临时视图,然后使用主查询查询该视图。创建此视图需要大量开销。通过两个查询,您将保持"查看"在 PHP内存中,这将大大减少开销。