Yii2框架的Active Record中select()语句的“AS”关键词不起作用,是什么原因?

今天在写一个接口的控制器的时候,需要把News表查询出来的数据中的id字段转换成news_id
于是我按Sql语句的写法直接调用了继承Active RecordNews模型,
结果查询出来的数据中id字段不见了。

$response = News::find()->select(['id AS news_id', 'news_title', 'news_content'])->all();
[
    {
        "news_title": "altestTitile",
        "news_content": "kasjdfljsdaf"
    },
]

如果直接使用QueryBuilder查询的结果,id字段如我所期待的结果,变成了news_id:

$response = (new Query())->select(['id AS news_id', 'news_title', 'news_content'])->from('tab_user')->all();
[
    {
        "news_id": "1",
        "news_title": "altestTitile",
        "news_content": "kasjdfljsdaf"
    },
]

出现这个问题究竟是我使用的方法不对,还是因为Yii2框架的某些Bug导致功能实现的不全?
如果有遇到或者了解类似问题的请帮忙解答一下。

阅读 4.2k
1 个回答

你试试看['id' => 'news_id', 'news_title', 'news_content']

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题