redGREENblue
redGREENblue

Reputation: 3126

Yii model not getting posted

I have this weird issue where the values seems to get posted on form submit but still there the controller seems to not find it. I am saving three models with a single form. Both the relations to the main model are HAS_MANY. Here's the controller.

$model=$this->loadModel($id);
$modelunitgroup = $model->userUnitgroups;
$modelunit = $model->userUnits;
    if(isset($_POST['User'], $_POST['Userunitgroup'], $_POST['Userunit']))
        {
            $model->attributes=$_POST['User'];
            $modelunit->attributes=$_POST['Userunitgroup'];
            $modelunitgroup->attributes=$_POST['Userunit'];
            $valid=$model->validate(); 
            if($valid)  
                {
                    if($model->save(false))  
                    {
                        $modelunitgroup->user_id = $model->id;  
                        $modelunit->user_id = $model->id;  
                        if ($modelunitgroup->save() && $modelunit->save())  
                            {
                                    Yii::app()->user->setFlash('success', "User updated!"); 
                                    $this->redirect(array('view','id'=>$model->id));
                            }
                    }
                }
        }
    $this->render('update',array(
    'model'=>$model,
    'modelunitgroup'=>$modelunitgroup,
    'modelunit'=>$modelunit,
    )); 

Here's the relations

'userUnitgroups' => array(self::HAS_MANY, 'Userunitgroup', 'user_id'),
'userUnits' => array(self::HAS_MANY, 'Userunit', 'user_id'),

And the form view

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

        <?php echo $form->dropDownListRow($model, 'title', $model->getUtitle(),array('prompt' => 'Select ...'));?>
        <?php echo $form->textFieldRow($model,'firstname',array('class'=>'span5','maxlength'=>60)); ?>
        <?php echo $form->textFieldRow($model,'lastname',array('class'=>'span5','maxlength'=>60)); ?>
        <?php echo $form->textFieldRow($model,'mobile',array('class'=>'span5')); ?>
        <?php echo $form->textFieldRow($model,'email',array('class'=>'span5','maxlength'=>160)); ?>
        <?php echo $form->passwordFieldRow($model,'password',array('class'=>'span5','maxlength'=>32)); ?>
        <?php
            foreach ($modelunitgroup as $mgroup) {
            echo $form->dropDownListRow($mgroup, 
            'unitgroup',
            $model->getUnitgroups(), 
            array(
            'ajax' => array(
            'type'=>'POST',
            'url'=>CController::createUrl('user/getunit'),
            'update'=>'#Userunit_unit',
            ))); 
            }
            foreach ($modelunit as $munit) {
            echo $form->dropDownListRow($munit, 
                'unit',
                array()
                );
            }
        ?>

I keep getting the error "Attempt to assign property of non-object". It seems that the 'Userunitgroup' and 'Userunit' are never posted, which is weird cause I checked the header in Firefox and all the values are correctly being posted. Any help on what may be causing this and how to solve this?

Upvotes: 0

Views: 996

Answers (2)

Developerium
Developerium

Reputation: 7265

I think you have a problem in your models rules in your scenario, in your model class:

public function rules() {
    array('title ,name, country, ..., password', 'safe', 'on'=>'thisCustomeScenario'),
    ...
}

and try to var_dump($_POST) to see what exactly your getting

Upvotes: 0

topher
topher

Reputation: 14860

$modelUnitGroup and $modelunit are arrays, yet you are assigning attributes like they are individual models (objects). For more information on how to do batch inserts/updates consult the manual.

Upvotes: 1

Related Questions