views:

132

answers:

2

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.
But 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!

+1  A: 

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(); ?>
thaddeusmt
Thanks! the form was correctly formed. The problem was that some fields on the model didn't have any validation rules and weren't declared 'safe' on the model either, so that's why the weren't mass assigned.
Petruza
ah, of course. glad you got it figured out! you should mark your own answer as the correct one, perhaps. cheers
thaddeusmt
A: 

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

Petruza