Yii Framework Forum: Best method for saving MANY_MANY records - Yii Framework Forum

Jump to content

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

Best method for saving MANY_MANY records Rate Topic: -----

#1 User is offline   tommytwoeyes 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-March 10

Posted 24 March 2010 - 08:45 PM

I've been searching/reading through the forum for a couple of days now, trying to figure out the best way to save records that are part of a MANY_MANY relationship.

I found a couple of posts describing ways to accomplish this, but one involved an extension and the other was a little difficult for me to follow.

I was hoping to find a way to save these records without having to install an extension (for the sake of learning how to do it with Yii).

I am creating a simple to-do list application for the purpose of learning more about Yii. In my project, there are Users and Projects. Users can have many projects, and projects can have many users.

When I create a new project, I'm having trouble figuring out the best way to associate it with the correct user. I don't have a model for the association/join table, so I'm not sure how to insert records into that table if ActiveRecord isn't aware of it (except through the composite key definitions in each model's relations() method).

Can anyone offer suggestions for this, please?

Thanks in advance!
Tom
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 March 2010 - 05:18 AM

You could add methods like addUser()/deleteUser() to your Project model that handle creation/deletion of the AR for the connecting table. That helps to keep your controller code clean.
0

#3 User is offline   tommytwoeyes 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-March 10

Posted 25 March 2010 - 06:53 AM

View PostMike, on 25 March 2010 - 05:18 AM, said:

You could add methods like addUser()/deleteUser() to your Project model that handle creation/deletion of the AR for the connecting table. That helps to keep your controller code clean.


Thanks man, that sounds good.

For one of them, I ended up putting the code to update the join table (with raw SQL) in the model's after save method.
Is there any way to add/update the MANY_MANY records using ActiveRecord?
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 March 2010 - 07:04 AM

Not sure if i understand. You can use AR in the addUser() method.
0

#5 User is offline   tommytwoeyes 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-March 10

Posted 25 March 2010 - 07:11 AM

View PostMike, on 25 March 2010 - 07:04 AM, said:

Not sure if i understand. You can use AR in the addUser() method.


No problem - I was just wondering if there is a way to create a User record in the database within the addUser() method using ActiveRecord's methods and model relationships rather than just writing the SQL needed to add the record in the join table.
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 March 2010 - 07:15 AM

Simply create a AR for your join table. Then you can do like this:

public function addUser($userid) {
  $user=new ProjectUser;
  $user->project_id=$this->id;
  $user->user_id=$userid;
  return $user->save();
}

0

#7 User is offline   tommytwoeyes 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-March 10

Posted 25 March 2010 - 07:20 AM

View PostMike, on 25 March 2010 - 07:15 AM, said:

Simply create a AR for your join table. Then you can do like this:

public function addUser($userid) {
  $user=new ProjectUser;
  $user->project_id=$this->id;
  $user->user_id=$userid;
  return $user->save();
}



Sweet, that's what I was hoping to find. Thanks very much for your help!

Tom
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