$model->attributes not assigning all $_POST fields

I have 5 fields in a model 3 of which I am capturing in _form and rest of the 2 are being set in beforeSave() method. In controller, when I do the following, it doesn’t assign all 3 variables to $model->attributes even though field names, database column names are same.


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

One thing I noticed is that it’s assigning text items properly however not assigning numeric fields. In my case, there is on text field and 2 integer fields. I have verified that all values are coming properly in $_POST but when it’s assigned to $model->attributes then integer values are not saved in model.

Any ideas why this could be happening? As of now, I am manually assigning those integer fields from $_POST values but that doesn’t seem the right way of doing it.

The attributes have to be considered safe for massive assignement. Setup validation rules for all of them.

I dont personally see. Please post your code.

Here is the controller code. parent_id and story_id are not been assigned when I am assigning $_POST to model attributes, comment is being assigned properly.


	public function actionCreate()

	{

		$model=new Comment;


		if(isset($_POST['Comment']))

		{

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

			if($model->save())

				$this->redirect(array('story/view','id'=>$model->attributes['story_id']));

		}


	}

Here is _form code.


<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'comment-form',

	'action'=>Yii::app()->createUrl('comment/create'),

	'enableAjaxValidation'=>false,

)); ?>

	<?php echo $form->errorSummary($model); ?>


	<?php echo $form->textArea($model,'comment',array('rows'=>6, 'cols'=>50, 'style'=>'width:98.5%')); ?>

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

	<?php echo $form->hiddenField($model,'story_id',array('value'=>$story_id)); ?>

	<?php echo $form->hiddenField($model,'parent_id',array('value'=>$parent_id)); ?>	

	<div class="pull-right">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class'=>'btn-large btn-primary')); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

And, here is model code.


	protected function beforeSave()

	{

	    if(parent::beforeSave())

	    {

	        if($this->isNewRecord)

	        {

	            $this->user_id=Yii::app()->user->id;

		$this->approved=Comment::STATUS_APPROVED;

	        }

	        return true;

	    }

	    else

	        return false;

	}

You were right. Making those attributes did the trick. It’s assigning now properly. THANKS!!

And your model validation rules?