Issue While Assigning Model Attributes

DB

id

name

parent_id (FK to id)

View


...

<div class="row">

    <?php echo $form->labelEx($model, 'parent_id'); ?>

    <?php

    echo $form->dropDownList($model, 'parent_id', CHtml::listData($model->findAll(), 'id', 'name'));

    ?>

    <?php echo $form->error($model, 'parent_id'); ?>

</div>

...

Controller


...

$model->attributes = $_POST['ProductCategory'];

echo 'From POST<pre>';

print_r($_POST['ProductCategory']);

echo '</pre>';

echo 'Model Atributes<pre>';

print_r($model->attributes);

echo '</pre>'; die();


/* Output

From POST


Array

(

    [name] => Home

    [parent_id] => 3

)


Model Atributes


Array

(

    [name] => Home

    [id] => 

    [parent_id] => 

)


*/

echo '</pre>';

...

As you can see when I assign $model->attributes, I am not getting the value in $model->parent_id. But $model->name is assigned with the corresponding value.

However if I assign $model->parent_id explicitly like this,


$model->parent_id = $_POST['ProductCategory']['parent_id'];

I am getting the value.

So, why the $model->parent_id is not set while assigning $model->attributes. What am I doing wrong? Or is this a bug?

Hi,

This is not a bug. This is the functionality provided by yii, because when u copy id also, then you will get duplicate key exception, so it avoids copying primary key.