The "AS" keyword of the select () Statement in ActiveRecord of the Yii2 framework does not work. Why?

Source: Internet
Author: User
When writing an interface controller today, You need to convert the id field in the Data queried from the News table to news_id. Therefore, I directly called the News model that inherits ActiveRecord according to the SQL statement, and the id field in the queried data is missing. {Code...}... when writing an interface controller today NewsThe idConvert fields news_id.
So I directly called the inheritance statement in the SQL statement format. Active RecordOf NewsModel,
The data is being queried. idThe field is missing.

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

If you directly useQueryBuilderQuery result,idThe field is changednews_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"    },]

Is the method I used incorrect or is the implementation incomplete due to some bugs in the Yii2 framework?
If you have any questions or want to know more about them, please help us.

Reply content:

When writing an interface controller todayNewsTheidConvert fieldsnews_id.
So I directly called the inheritance statement in the SQL statement format.Active RecordOfNewsModel,
The data is being queried.idThe field is missing.

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

If you directly useQueryBuilderQuery result,idThe field is changednews_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"    },]

Is the method I used incorrect or is the implementation incomplete due to some bugs in the Yii2 framework?
If you have any questions or want to know more about them, please help us.

Try ['id' => 'news _ id', 'news _ title', 'news _ content']

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.