Lazy and eager loading... Trying to get the best mix.

Hi all

I've met one little problem:

<?php $record = Record::model()->findByPk($id);

The code above will fetch all record attributes, but what if I don't need them all?

Actually I need the single attribute and I have no need to get all the attributes from database. It decreases performance as you know.

Can anyone help me with this point? I’m sure that a solution exists, but it is not clear to me… ???

Hy there,

take a look at the database - section in "the definitive guide to Yii"…


You could define the attribute you need in the select property of your DB - criteria and use find() instead of findByPk()

$criteria = new CDbCriteria;

$criteria->select = 'ATTRIBUTE_NAME';

$criteria->condition = 'id=:id';

$criteria->params = array('id'=>10);

Hope this helps you…

Thanks a lot!

I still haven't check it, but I think that is obviously what I actually need.