Yii Framework Forum: Multiple Relations Between Two Tables In Yii - Yii Framework Forum

Jump to content

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

Multiple Relations Between Two Tables In Yii Rate Topic: -----

#1 User is offline   Sukhwinder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 27-October 09

Posted 03 December 2012 - 02:39 AM

hi
I have two tables, User and Post. My required set up is as under :

A User can make Posts
A User can reply a Post.
A user can add a Post to his To DO List
A user Can follow a Post (to get notification when some one update/reply a post)
A notification tables to store notification e.g UserID,PostId,Notification_status etc (As in facebook notifications)

Now as we can see that multiple relations are required between User and Post tables, How can i Implement this in Yii, as for as mySql is concerned, i think i need to have a separate table for each relation. eg one table "Followers" having field UserId,PostID etc another tables "TODOs" having fields "postID , UserID etc and so on
is this the correct method or some other better alternate? how to define these things in Rletation Method in Yii Models?

Thank and Regards
0

#2 User is offline   YearningForYii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 21-September 12

Posted 03 December 2012 - 03:06 AM

You need to define rules in your model class.For e.g
A user can make many posts .The relation in the User class will be as follows.:

public function relation()
{
return array(
'posts'=>array(self::HAS_MANY,'POSTS',post_id);
);
}
And then you can access comments in the view as $data->posts->postname;
where dataProvider is of User Model class.



A User can make Posts
A User can reply a Post.
A user can add a Post to his To DO List
A user Can follow a Post (to get notification when some one update/reply a post)
A notification tables to store notification e.g UserID,PostId,Notification_status etc (As in facebook notifications)

Now as we can see that multiple relations are required between User and Post tables, How can i Implement this in Yii, as for as mySql is concerned, i think i need to have a separate table for each relation. eg one table "Followers" having field UserId,PostID etc another tables "TODOs" having fields "postID , UserID etc and so on
is this the correct method or some other better alternate? how to define these things in Rletation Method in Yii Models?

Thank and Regards
[/quote]
0

#3 User is offline   PeRoChAk 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 570
  • Joined: 26-November 10
  • Location:Lahore, Pakistan

Posted 03 December 2012 - 03:07 AM

This will be same as connecting one table to other i.e. Building one relation between tables.

You can see that relations are in the form of array, means a table can have any number of relations...

Thanks
Cheers
Perochak
Web Design - Logo Design - Application Development

My Blog | Forums Comparison
0

#4 User is offline   Sukhwinder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 27-October 09

Posted 10 December 2012 - 02:23 AM

I am re-posting with more details ::
A Post Belongs to a User.
A Post may be addressed to one or Many Users
Users can follow a Post to receive notification when there is some reply/comment on a post.
A User can add a Post to his ToDo list and when he completes this ToDo he can mark this ToDo Done (so this ToDo be removed from his ToDo list)
Users Should get notification when a post is addressed to a User, or when some reply/comment is given on Post the User is Following. (A user who create the Post , or A user who is addressed in Post, will be automatically following that Post).

So This way i need to create different many many relations ships between Post and User tables, so do i Need to create a different joining table for each relation? like Post_user_follower(id,PostId,UserId) to save the data of Users who are following some Post, and so on for each relation, is this a normal case or there will be circular joins creating infinite loops?
can i save additional data in the joins tables along with the Id's of joining tables e.g for ToDO's, i will create a table Pots_user_toDo (id,userId,PostId,toDo_status) so that i can save the status of ToDO in the join table to check whether the ToDO is pending or completed, or i need to save the ToDo_status in another tables e.g ToDo(id,toDO_status)
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