Saving Data To Multiple Tables Using One Form

Hi all,

I’m trying to save data in two tables from one form. I have three tables users,projects,and relations. Now in reations table I have sourceID,relation,targetID as fields. The idea is when a user creates project his id will be saved in relations table as sourceID and th project’s id as targetID. And relation would be “Creator” or “Owner”.Any ideas on how to implement this?

Thanks in advance.




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

		{


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


			if($model->save()){


				$relation = new Relation; 


				$relation->targetID = $model->projectId; //project's Id the one we just saved

				$relation->sourceID = Yii::app()->user->userId; //user's Id, if you have a login

				$relation->save();




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

			}				

		}



i hope this helps

I suggest the following code:

At the begining of the controller:

private $_relation;

In the create’s method:

$model=new Project;

if(isset($_POST[‘Project’]))

{

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


if($model->save())


    {


        $this->_relation = new Relation;// Model of Relations


        $this->_relation->attributes=$_POST['Project'];


        if( $this->_relation->save()) {


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


        } else {


           throw new CHttpException(404, 'Your error message!.');                                                                 


        }

I hope this code help you

Hi and thanks for the swift reply.

It worked like magic. I have posted the working code below. Now that this worked I will put it into use for the other models in the project.

public function actionCreate()

{


	$model=new Project;


	$_relation =new Relation;


	





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





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


	{


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


		


		if($model->save()){


	





                            $_relation->targetID = $model->project_id; //project's Id the one we just saved


                            $_relation->sourceID = $model->user_id; //user's Id, if you have a login


                            $_relation->relation = 'Owner';


                            $_relation->save();








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


                    }                               


	}





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


		'model'=>$model,


	));


}

The idea behind this is anything that is created by the user will be saved in the “relations” table with the user’s id as the sourceID, the relation as “Owner”,“creator” and the targetID as the id of what the user has created be it project,course, or group.

again thanks for the reply

Just a comment here. I saw this topic by chance but it’s really valuable to me because in what I’m doing I’ll need to save to as many as 6 tables on one form. That drops to 4 if joins to items that work like tags (but aren’t tags) are excluded. It is a carefully thought out but complex data plan & it would make for a really bad UI if I sent users to different forms for each object in the data.

Cheers all :)