YII Framework, PHP - how to get element of CActiveRecord array

207 views Asked by At

When I dump all variables of a model in controller, I can see:

[CActiveRecord:_attributes] => array
    (
        'id' => '524'
        'version' => '0'
        'created' => '2015-06-24 12:37:27'
        'documents_id' => '528'
    )

How can I get for example 'documents_id' element and use it in that controller?

2

There are 2 answers

0
Moutasem Shahin On

You have two ways:

1- Convert the Object to array

function convertObject2Array($object){
    $finalArray = array();
    foreach($object as $propertyName => $propertyValue)
        $finalArray[$propertyName] = $propertyValue;
    return $finalArray;
}

2- Use Yii::app()->db->createCommand instead of $this->loadModel($ID) like the followings:

$SQL_II = "SELECT * FROM users WHERE id = '" . $ID . "'";
$dbCommand = Yii::app()->db->createCommand($SQL_II);
$finalArray = $dbCommand->queryAll();
0
Anamika Shrivastava On

You can get attribute value of model by model object.

$modelobj = new Modelname();

$modelobj->documents_id;