ViewModel not in $_POST on form submission

I have a form page that uses the following ViewModel:


class UserEditForm extends CFormModel

{

    public $user;

    public $positions;

    

    public function rules()

    {

        return array(

            array('username, password', 'required'),

            array('user, positions', 'safe')

        );

    }

    

    public function attributeLabels() {

        parent::attributeLabels();

    }

    

    

}

The $user property of the ViewModel is itself a model of type UserModel.

UserModel contains $positions which is an array of type PositionModel.

Each PositionModel has a child OrgModel

In my controller I populate the ViewModel, and each of the nested Models within it appear correctly populated as well. Here is my controller action:


public function actionEdit($id = null)

        {

            $model = new UserEditForm;

            if($id != null)

            {

                $model->user = UserModel::model()->findByPk($id);

                $model->user->positions = $model->user->getRelated('positions');

                foreach($model->user->positions as $pos)

                {

                    $pos->org = $pos->getRelated('org');

                }

            }

            

            // if it is ajax validation request

            if(isset($_POST['ajax']) && $_POST['ajax']==='userEditForm')

            {

                    echo CActiveForm::validate($model);

                    Yii::app()->end();

            }


            // collect user input data

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

            {

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

                // validate user input and redirect to the previous page if valid

                if($model->validate())

                {

                    //$this->redirect(Yii::app()->user->returnUrl);

                    //TODO: throw error

                }

            }

            

            $this->render('edit', array('model' => $model));

        }

And all my data is correctly displayed on the View page. It is even looping through the multiple PosiionModels and displaying both their data, and the data in each PositionModel’s child OrgModel correctly.

The issue is that when I change a field in the UserModel, say ‘firstName’ in the form on the View and submit, in the $_POST vars, I don’t receive my ViewModel, I recieve a UserModel, one PositionModel, and one OrgModel associated with the PositionModel, but these models are all on the same level, not nested, and where there should be multiple PositionModels and OrgModels, I’m getting only one.

What do I need to do to get my whole ViewModel back?

If I need to post my View, I can, but I didn’t want to drop too huge a block of code.

Any help would be greatly apprecaited.

Thanks,

Jason

I think I may be onto something.

Since my ViewModel is only holding other Models and doesn’t really have any properties of its own, In my view, all the fields are like this:


<div class="control-group">

                                <?php echo $form->labelEx($model->user, 'password', array('class' => 'control-label', 'for' => 'password')); ?>

                                <div class="controls">

                                    <?php echo $form->textField($model->user, 'password'); ?>

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

                                </div>

                            </div>

So, the issue seems to be that in the ACactiveRecord methods, I’m passing the ViewModel’s member Models as the model, instead of the ViewModel itself.

That begs the question, if I specify the ViewModel as the first method argument, how do I refer to a property of my ViewModel’s member Models?

Something like one of these?




<

?php echo $form->textField($model, 'user->password'); ?>

<?php echo $form->textField($model, 'user.password'); ?>

<?php echo $form->textField($model, 'user["password"]'); ?>



?