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

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

WBOY
WBOYOriginal
2016-06-06 20:14:231542browse

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

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

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

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

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

回复内容:

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

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

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

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

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

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn