Yii - 从模型中获取属性

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