简体   繁体   中英

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

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?

You can get attribute value of model by model object.

$modelobj = new Modelname();

$modelobj->documents_id;

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM