user2696439
user2696439

Reputation:

How to select certain colums in yii CActive record?

I want to get all rows , certain columns using CActive record

I heard about findAll(); but it returns all rows, colums

$models = Users::model()->findAll();

How to get certain columns for example, only username, password for all users?

Upvotes: 2

Views: 113

Answers (4)

Jenno Richi Benat
Jenno Richi Benat

Reputation: 671

You could use as usual sql query but getting so would not be in yii standard

So get findAll() and get the data that you want. that is proposed and best.

Upvotes: 0

Alireza Fallah
Alireza Fallah

Reputation: 4607

the Kumar_v 's answer is completely correct, but if you want only username and password (or any two fields),

you can also use CHtml::listData() function like this :

 $users=CHtml::listData(Users::model()->findAll(array('condition'=>'*condition*',
'limit'='*limit*')),"username", "password");

the $users is an array like this :

$users = array(
'username1'=>'password1',
'username2'=>'password2',
.
.
.

);

and of course you can access to a username's password simply by $users['username1']

Upvotes: 0

Let me see
Let me see

Reputation: 5094

If you want the result in the form of array try this

$records= Yii::app()->db->createCommand()
                    ->select('username,password')
                    ->from('users')
                    ->queryAll();

If as an object then kumar_v has already given an answer

Upvotes: 1

Kumar V
Kumar V

Reputation: 8838

You can do with CDbCriteria

$criteria = new CDbCriteria;
$criteria->select = 't.username, t.password '; // select fields which you want in output
$models = Users::model()->findAll($criteria);

Upvotes: 7

Related Questions