简体   繁体   中英

Yii - getting the attributes from a model

I have a model that has data set dynamically using the following:

$array = array('user_id' => 12345);
$model->setAttributes($array);

But when I run the following on the next line down it returns null?

echo $model->user_id; // i would expect to see 12345

Can anyone explain why this is null?

setAttributes will only work on attributes that have rules set on it. If there is no rule set for the attribute user_id , then you need to put it under safe rule. This is related to Massive Assignment .

$array = array('user_id' => 12345);
$model->setAttributes($array);

This one define the model

echo $model->user_id;

is printing the record from database having column name user_id.

<?php echo CHtml::beginForm(); ?>

<?php echo CHtml::activeLabel($model,'username'); ?>

<?php echo CHtml::endForm(); ?>

This code only printing the label which you defined "12345"

The "NULL" returns, possibily there is no data in the column

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