Home  >  Q&A  >  body text

php - Can the objects detected by Yii2 AR be accessed using array mode? ?

$infoModel = InfoModel::findOne(1);
The above is to find the data with id 1. I have printed $infoModel, which is an object.
However, the correct data can be printed successfully by outputting the following two sentences:

echo $infoModel -> name; //这个可以理解   他是对象可以->
echo $infoModel['name']; //这个不理解  他不是数组啊

The following is the printed $infoModel:

app\models\InfoModel Object
(
    [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 1
            [name] => 我问问2
            [age] => 32
        )

    [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 1
            [name] => 我问问2
            [age] => 32
        )

    [_related:yii\db\BaseActiveRecord:private] => Array
        (
        )

    [_errors:yii\base\Model:private] => 
    [_validators:yii\base\Model:private] => 
    [_scenario:yii\base\Model:private] => default
    [_events:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => Array
        (
        )

)

Also, by the way<?php echo $form -> field($infoModel,'name')->textInput(array('value' => $infoModel['name'])); ?>
Is the first parameter $infoModel of the field here necessarily a model object? I tried to convert $infoModel into an array in the controller and an error occurred? ? ?

给我你的怀抱给我你的怀抱2690 days ago879

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-06-23 09:13:24

    AR can be accessed in array mode because it implements the interface ArrayAccess. If you want to extract the data content, you can use the model’s getAttributesmethod

    reply
    0
  • Cancelreply