來源:互聯網
上載者:User
關鍵字
yii2
php
yii
php架構
mysql
今天在寫一個介面的控制器的時候,需要把
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導致功能實現的不全?
如果有遇到或者瞭解類似問題的請幫忙解答一下。
回複內容:
今天在寫一個介面的控制器的時候,需要把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']