selecting only specified attributes of rows selected

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…

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

should return

array(‘rajat@gmail.com’,‘rajatsinghal@gmail.com’)…

$active_users_emails_ids = User::model()->findColumns(array(‘user_id’,‘email’), ‘active = 1’);

should return

array(

   ['user_id'] => array('1','2'),


   ['email'] => array('rajat@gmail.com','rajatsinghal@gmail.com')

)…

There is queryColumn() - http://www.yiiframework.com/doc/api/1.1/CDbCommand#queryColumn-detail

Thank you for the information about the queryColumn, but still it’s not available with criteria and findAll syntax which is much handy to people…To use it, one would have to write a query, which is not a feasible solution some-times, and it breaks the flow…

I created a small behavior for Ar which supports findColumn behavior…

github.com/rajatsinghal/CAdvancedArFindBehavior

There is absolutely nothing wrong with writing queries in an MVC application.

ActiveRecord is only designed for simple queries… basic CRUD operations. If you try to get too much out of it, while it may work it will be a big performance hit.

Get your hands dirty and write some clean, efficient SQL.

Very handy extension for retrieving a single column from all returned models.

Thanx rajat.developer