【问题标题】:Sonata admin createQuery ignores table alias in sort expressionSonata admin createQuery 忽略排序表达式中的表别名
【发布时间】:2018-09-28 21:42:58
【问题描述】:

我有两个实体:“OriginNews”和“FollowUpNews”。它们是一对多相关的,因此一个 OriginNews 可以有多个 FollowUpNews。

在我的 OriginNewsAdmin 中,我只想在 listview 中显示 OriginNews,而且还通过 FollowUpNews 排序(如果已连接)...所以我决定通过像这样更改 createQueryMethod 来操作我的 listView-data。

 /**
 * @param string $context
 * @return ProxyQueryInterface
 */
public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);

    $query->leftJoin('o.followUpNews', 'fun')
        ->addSelect('COALESCE(fun.importDate, o.importDate) AS HIDDEN sortDate')
        ->addOrderBy('sortDate');

    return $query;
}

这应该将 followUpNews 加入 originNews 并按 importdate 排序......当我运行它时,我会得到错误。

在渲染模板期间引发了异常 ("[Semantical Error] line 0, col 94 near 'sortDate ASC,': 错误: 'sortDate' 未定义。”)。

奏鸣曲管理员是在开玩笑吗?我在上面一行定义了 sortDate 字段……即使我删除了“HIDDEN”关键字……没有变化! 问题出在哪里?

我的目标是,始终将具有最新 FollowUpNews 的 OriginNews 放在首位……这可能以另一种方式实现吗?

谢谢

【问题讨论】:

    标签: doctrine sonata-admin dql


    【解决方案1】:

    搜索我的问题的几个小时我发现,这是一个简单的教义错误,已经存在了好几年......

    我遇到了与这里描述的相同的问题

    Doctrine Querybuilder ORDER BY clause is not in SELECT list

    我还通过将 sql 模式设置为 null 来解决此问题。我还安装了学说扩展,以按子句顺序使用 IFNULL 函数。

    我的 sql 现在如下所示

    $query->leftJoin('o.followUpNews', 'fun')
        ->addOrderBy(IFNULL('fun.importDate', 'o.importDate'), 'DESC');
    

    这对我有用。

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2019-04-21
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多