14

I often face this problem..

Lets say ..In blog application, I need to email all the active users..

What I do is write findAll on users with some conditions of their last login being greater than some value..and get all the User objects...Then run a foreach through all the user model objects and store emails in a array, then use the array..

I other words what is happening in back-end is I am loading whole model, while I only need barely0.5% of that information, and then running a dirty code to get values in in array, and then process with it..

Isn't it quite bad in performance and dirty code..

Now other approach I can think of is using commandBuilder and write query and then run same dirty code to get values in array..one problem of performance resolved..but as people say writing sql in mvc frameworks, is not a really good idea..

What I really want...some similar functions which give me column values in array if it is a single column, or array with column name as index if multiple columns..

So what I am thinkin of doing is implement it by extending ActiveRecord or something similar, what I want to check is if some one has already implemented something similar, or have some ideas for it..:)

2
  • Is that what you are looking for?yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes Commented Jul 19, 2012 at 18:20
  • 5
    Probably he needs to select some fields instead of all fields. Use the CDbCriteria::select to get which fields you need. Commented Jul 19, 2012 at 18:40

6 Answers 6

15

You must study of CDbCriteria deeply, their is all the things what your looking.

Have an example::

$criteria = new CDbCriteria;
$criteria->select = 't.first_name, t.email'; // select fields which you want in output
$criteria->condition = 't.status = 1';

$data = Users::model()->findAll($criteria);

$data is also an array output.

Sign up to request clarification or add additional context in comments.

4 Comments

Please clear your question, i replied on the basis of what i understood.
why settings 't.first_name' but not 'a.first_name'. I tried 'a.first_name' but it didn't work unlike the first one. Is it some kind of convention?
Alex, It's a default table alias. Yii creates queries like "SELECT t.id FROM table t", but it may vary depends on query complexity (multiple tables queries). Check yiiframework.com/doc/api/1.1/CActiveRecord#getTableAlias-detail
@Копать_Шо_я_нашел remove alias
14

In Yii2, you can use like this:

User::find()->select(['field1', 'field2']);

3 Comments

This should be the approved answer
@realtebo Yes this should be the approved answer but Question asking upon yii1 not yii2
Say you do $data = User::find()->select(['field1','field2']); and then you print $data->attributes. You'll find all the columns of the table will null values.
11

I created the behavior as I said, the way it works is:

$active_users_emails = User::model()->findColumn('email', 'active = 1');

**returns array('[email protected]','[email protected]')..**

Now one doesn't need to go through all activerecords and collect column in array, then proceed, and also bear weight of loading whole column..

You can find the extension on git ..https://github.com/rajatsinghal/CAdvancedArFindBehavior

Comments

6

This would also work without any behavior or without the addition to CActiveRecord. Nevertheless I also like the solution with the behavior.

$active_users_emails=CHtml::listData(User::model()->findAllByAttributes(array('active'=>1)), 'id', 'email');

Comments

2

Need to filter the model attributes using array_filter function. Try like this, its working great -

$criteria = new CDbCriteria;
$criteria->select = 't.first_name, t.email'; // select fields which you want in output
$criteria->condition = 't.status = 1';

$data = Users::model()->findAll($criteria);


Use array_filter before assign.

foreach($data as $model){
      $rows[] = array_filter($model->attributes);
   }

echo CJSON::encode($rows)


So, you will get output like this -

[{"first_name" : "Rohit", "email" : "[email protected]"}, {"first_name" : "Rajat", "email" : "[email protected]"}]

Comments

1

In same situation as question describes. After trying all above answers and some other resources here is what I am doing.

$emailIds = Yii::app()->db->createCommand(
                 'SELECT email FROM users WHERE is_active = 1'
            )->queryAll();

$emailIds = array_column( $emailIds, 'email' ) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.