时间:2021-07-01 10:21:17 帮助过:107人阅读
News表查询出来的数据中的id字段转换成news_id。Active Record的News模型,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导致功能实现的不全?
如果有遇到或者了解类似问题的请帮忙解答一下。
今天在写一个接口的控制器的时候,需要把News表查询出来的数据中的id字段转换成news_id。
于是我按Sql语句的写法直接调用了继承Active Record的News模型,
结果查询出来的数据中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导致功能实现的不全?
如果有遇到或者了解类似问题的请帮忙解答一下。
你试试看['id' => 'news_id', 'news_title', 'news_content']