How to select the few columns from the Database row

Hi Friends,

When selecting a few columns from the the particular table YII returns all the column name and values too.But for non selected columns comes as null values.


 $usercriteria = new CDbCriteria();                

 $usercriteria->select=="name,dob,place"

 $usercriteria->condition="user_id=1";

 $usersendemailmodel=TblUser::model()->findAll($usercriteria);



It returns the result,but it returns all columuns.For example I dont need the nativeplace,speakinglanguage from that table.But it returns that columns also.Name,dob,place contains the value.But the nativeplace,speakinglanguage columns comes with the null value.

I want to avoid again checking for the columns.If the result is comes with the expected columns I can push it into the array and I can use it in the front end.

Is I am doing anything wrong here or Is there any other way to avoid it.

P.S

I am using Yii Framework [color="#808080"][font="Verdana"][size="2"]1.0.7[/size][/font][/color]

You are doing everything ok and it is the default behaviour of ActiveRecord in Yii. Only specified values are fetched from database, however Model has more attributes because the table has more columns and they are in this case NULLed. It can’t work other way as there can be triggers (in example afterFind) which rely on those other attributes and would raise exception if they weren’t found in object at all. if you need only those 3 columns - use default DAO object and execute simple query.

I ran into the same problem as well. Following the Yii REST tutorial here, the list all example was returning all of the model’s columns, including those that I didn’t want to show. What I did was strip out the null values before returning the result using something like this




$users = User::model()->findAll();

$rows = array();

foreach($users as $user)

{

	$rows[] = array_filter($user->attributes);

}