开发者

Getting form data in Yii to a CActiveRecord model works for one model but not for another

开发者 https://www.devze.com 2023-01-08 11:11 出处:网络
I\'m getting a submitted form in this way: $resume->attributes = $_POST[\'ResumeModel\']; $profile->attributes = $_POST[\'UserProfile\'];

I'm getting a submitted form in this way:

$resume->attributes = $_POST['ResumeModel'];

$profile->attributes = $_POST['UserProfile'];

Both CActiveRecord models are correctly populated before this from the corresponding tables, they have the correct data and all.

Both models' data is present on $_POST as modified by the form.

B开发者_StackOverflow中文版ut it seems that the assignment to the attributes property works only for $profile and not for $resume.

If I check their values after that assignment, $profile doesn't get the edits from the form. Is there something in the definition of the model that can cause that? As far as I can see, both models are similarly implemented

I don't understand why this happens, does anyone have a clue?

Thanks!


The problem is that some fields on the $resume model didn't have any validation rules and weren't declared as safe either, so they couldn't be safely mass assigned.

Reference: http://www.yiiframework.com/doc/guide/form.model#securing-attribute-assignments


Have you checked the $_POST variables closely? For the mass "attributes" assignment to work the array should be of the form:

$_POST = (
  'ResumeModel' => (
    'data1' => 'something',
    'data2' => 'something else',
  ),
  'UserProfile' => (
    'data3' => 'yo ho ho',
    'data4' => 'bottle of rum',
  )
)

If it looks like this it's wrong:

$_POST = (
  'ResumeModel' => (
    'data1' => 'something',
    'data2' => 'something else',
    'data3' => 'yo ho ho',
    'data4' => 'bottle of rum',
  )
)

To ensure that the form is building the correct $_POST array for each model, make sure that you are passing both the $resume and $profile model into your form View like this:

<?php 
$resume=new ResumeModel;
$profile=new UserProfile;
$this->render('yourFormView', array('resume'=>$resume,'profile'=>$profile));
?>

Then, in "yourFormView", make sure that you are creating the form fields appropriately with each model, like so:

<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php echo $form->textField($resume,'data1'); ?>
<?php echo $form->textField($resume,'data2'); ?>
<?php echo $form->textField($profile,'data3'); ?>
<?php echo $form->textField($profile,'data4'); ?>
<?php $this->endWidget(); ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消