How to get model data like array


(Holala) #1



$criteria=new CDbCriteria;

$criteria->select='connect_to';

$criteria->condition='employee_id=:employeeID AND group_id=:groupID AND lev=:lev';

		$criteria->params=array(':employeeID'=>$employeeID,':groupID'=>$groupID,':lev'=>$lev);

return self::model()->findAll($criteria);



method findAll return a object array,dose it has a way can make object array to normal array just like $data[0]=1;$data[1]=2;

Thanks!


(Jeff) #2

CActiveRecord implements (via CModel) ArrayAccess, so if $data is the result of findAll() the following two lines are equivalent:


echo $data[0]->employee_id;

echo $data[0]['employee_id'];

Is that what you mean?


(Rohit N Sarpotdar) #3

$obj=self::model()->findAll($criteria);

$arr=$obj->attributes;

model() method returns Returns the static model of the specified CActiveRecord class.

Check CActiveRecord propertiesā€¦ "attributes"


(Sacoka) #4

Solved here and here.


(Prajapatidhara) #5

Here is my problem in rights module.

$data = array();

                    $sql = "SELECT * FROM authitem WHERE created_by =".Yii::app()->user->id;


                    $dbCommand = Yii::app()->db->createCommand($sql);


                    $data = $dbCommand->queryAll();

This gives me normal array but i want model array of authitem. is there any solution?


(Joey Black2008) #6

queryOne() is best used when only the first row of result is needed for a query.