Yii Framework Forum: Saving Data To Multiple Tables Using One Form - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Saving Data To Multiple Tables Using One Form Rate Topic: -----

#1 User is offline   zatmont 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 15-March 12

Posted 15 February 2013 - 12:38 PM

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.
0

#2 User is offline   FlyngThunderGod 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 29-January 13

Posted 15 February 2013 - 01:03 PM

		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
0

#3 User is offline   sanat 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 26-September 12
  • Location:Caracas, Venezuela

Posted 15 February 2013 - 02:01 PM

View PostFlyngThunderGod, on 15 February 2013 - 01:03 PM, said:

		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
0

#4 User is offline   zatmont 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 15-March 12

Posted 17 February 2013 - 10:44 AM

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
1

#5 User is offline   Pedro fp 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 18-June 11

Posted 17 February 2013 - 12:09 PM

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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users