Select a specific column value with a condition

I want to get a specific column from a user table in yii2 using the active entry below my code

$model = User::findOne(['id' => 1]); 

this will return the entire column from the table with the user id equal to 1, but suppose I just want to get only the username and email address from this column. How can I write a query with an active record, I tried the code below, but it will not work .. '

 $model = User::find('username','email')->where('id'=1) 
+9
source share
4 answers

Try the following:

 $model = User::find() ->select('column1, column2') ->where(['id' => $id]) ->one(); echo $model->column1; 
+12
source

Just try:

 $model = User::find()->select(['username','email'])->where('id=1')->One(); 

OR

 $model = User::find()->select(['username','email'])->where('id=:id', [ ':id' => 1 ])->One(); 

The second option is more preferable.

+2
source
 $model = User::find() ->select(['column1', 'column2']) ->where(['id' => $id]) ->one(); 
0
source

Below the path is correct. but others filed as NULL.

 $model = User::find()->select(['username','email'])->where('id=1')->One(); 

The best way is to use cdb criteria for the query.

0
source

All Articles