Zabs
Zabs

Reputation: 14142

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?

Upvotes: 0

Views: 390

Answers (2)

nazim
nazim

Reputation: 1579

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.

Upvotes: 3

Akilan
Akilan

Reputation: 1717

$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

Upvotes: -1

Related Questions