Create one model and dynamically update second model

Hello :) I have a little problem with creating one model and updating second model in one time.

I have two models: Works and Rooms. In Rooms model i have fields: roomId, roomname, area, is_completed, complete_year, complete_month. In Works model i have fields: id, type_of_work (for example painting), date_of_work, room_id. I would like to create a new work and have the ability to automatically set whether the room is marked as completed (in one form). I’ll just add that when I create a new work (create action), I don’t have immediately access to the room id, because it’s set by completing the form when a new work is added (from dropdown list). Can somebody help me with this? I know how to create a one form for two NEW models - but I do not know what to do in this particular situation - when one model is created but second exist:(

Thank you in advance for your help

Sorry for my english:(


look at the extension wform with this extension your request should be possible.

otherwise all you can do is validate both models by hand where the foreign key in the works model isn’t set to required so it doesn’t produce an error.

And then save the Room model and on success also the works models.

You can also use transactions when saving and do a roolback of everything if an error occurs.

Let’s start from the beggining. It’s create action for my Work model:

    public function actionCreate() {

        $model = new Work;

        if (isset($_POST['Work'])) {

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

            if ($model->save()) {                    

                $modelRoom = Room::model()->findByPk($model->id_room);  

                $modelRoom ->is_completed = $_POST['Work']['is_completed'];

                $modelRoom ->year_completed = $_POST['Work']['year_completed'];    

                $modelRoom ->month_completed = $_POST['Work']['month_completed'];                

                $modelRoom ->save();


               $this->redirect(array('view', 'id' => $model->id));              



        $this->render('create', array(

            'model' => $model,



Code below save my new work and update room model But the problem is, that if there are any validation error for Room model i can’t display it, before saving Work model.

I don’t know if it’s a good idea, but i created three additional attributes in Work model: is_completed, year_completed, month_completed, so i can add this fields to my create view form

This is my shortened view file for creating new Work:

<?php echo $form->dropDownListRow($model, 'roomid', $model->getRooms(), array('class' => 'input-large', 'maxlength' => 2, 'prompt' => 'Please choose:')); ?>

echo $form->checkboxRow($model, 'is_completed');

echo $form->dropDownListRow($model, 'year_completed', Work::getYears(), array('class' => 'span2', 'prompt' => 'Please choose:'));

echo $form->dropDownListRow($model, 'month_comleted', Work::getMonths(), array('class' => 'span2', 'prompt' => 'Please choose:')); 

When i try to save this form, this fields: is_completed, year_completed and month_completed are not validated (bacause there are no validation rules in Work model - there are in Room model).

What am I doing wrong …:(

Please help :)



public function actionCreate() {

    	$model = new Work;

    	$room = new Room;

    	if (isset($_POST['Work'], $_POST['Room'])) {

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

        	$room->attributes = $_POST['Room'];

        	// validate BOTH Work and Room

        	$valid = $model->validate();

        	$valid = $room->validate() && $valid;

        	if ($valid) {

            	$room = $model->id;

            	$room->is_completed = $_POST['Work']['is_completed'];

            	$room->year_completed = $_POST['Work']['year_completed'];

            	$room->month_completed = $_POST['Work']['month_completed'];



            	$this->redirect(array('view', 'id' => $model->id));



    	$this->render('create', array(

        	'model' => $model,

        	'room' => $room



make your form accordingly… Here is a how to

also, I’d make complet year and complete month one date field.

Thanks for reply. But i don’t want to create “new Room” in a create action, because Room model (and Room’s data’s) exist before (as dropdown list). I only want to update is_completed, year_completed, month_completed field in existed before Room record. In this situation this solution wouldn’t work ?