0

我有一个基于学说的应用程序,我想从两个单独的表中选择行并按创​​建日期对它们进行排序。我现在(有点)以这种方式完成它:

$query = $em->createQueryBuilder()
  ->from('Embed', 'e')
  ->select("e")
  ->leftJoin("User", "u", "WITH", "e.uid=u.id")
  ->leftJoin("Product", "p", "WITH", "e.pid=p.id")
  ->where("u.image > 0 OR p.image > 0")
  ->addOrderBy("u.timeCreated + p.timeCreated", "DESC")
  ->setMaxResults(28)
  ->getQuery();

我想混合产品和用户数据,按创建日期排序,但排序不正确。我还尝试使用两个 addOrderBy 语句,它们正确地对用户和产品进行排序,但将所有用户放在结果中的首位,而不是混合它们。

编辑:Rufo 爵士采用了正确的方法 - 这是工作代码:

$query = $em->createQueryBuilder()
  ->from('Embed', 'e')
  ->select("e")
  ->leftJoin("User", "u", "WITH", "e.uid=u.id")
  ->leftJoin("Product", "p", "WITH", "e.pid=p.id")
  ->addSelect('COALESCE( u.timeCreated, p.timeCreated ) as timeCreated')
  ->where("u.image > 0 OR p.image > 0")
  ->orderBy("timeCreated", "DESC")
  ->setMaxResults(28)
  ->getQuery();
4

1 回答 1

3

你应该试试这个

$query = $em->createQueryBuilder()
  ->from('Embed', 'e')
  ->select("e")
  ->leftJoin("User", "u", "WITH", "e.uid=u.id")
  ->leftJoin("Product", "p", "WITH", "e.pid=p.id")
  ->where("u.image > 0 OR p.image > 0")
  ->addOrderBy("COALESCE( u.timeCreated, p.timeCreated )", "DESC")
  ->setMaxResults(28)
  ->getQuery();
于 2012-12-17T23:58:40.403 回答