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?
$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
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()
$_relation =new Relation;
// Uncomment the following line if AJAX validation is needed
$_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';
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.
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.