What I need:
A facebook-like friendship system.
User (A)
sends a requests for a friendship withUser (B)
User (B)
confirmes the requestUser (A)
andUser (B)
are friends now
My problem:
I'm confused how to work this out. I read a lot on the internet but it did not really helped me...
My Questions:
- What kind of link is it in CakePHP? Is it hasAndBelongsToMany? Or hasMany? Or ...?
- How do I realise it in the datebase correctly?
- How do I link it in the model?
What I already did:
Table: users
Name: id
, username
, password
, ...
users_users table: id
, user_id
, friend_id
, approved
Model:
'User' => array(
'className' => 'User',
'joinTable' => 'users_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
);
"@tereško Thank you! But I get an error: Error: An Internal Error Has Occurred"
First to answer your comment about the "internal error" you getting:
Try setting
debug
to 2 inconfig.php
you will realize that you will start getting much more understandable errors.Regarding your first question: 1. Your relation basically looks to like hasMAny since every user has friends. HABTM will also work here, but it is much more complicated. This decision (what relations to use) also depends on other parts of your system - i.e. for what and how you'd like to use this data in other parts of it. 2. Read here 3. Read there again
A bit more on HABTM and hasMany through the jon model (if you need to store any additional data in the join table while using the same idea as HABTM).
Your current DB structure is awful. I didn't get why you need a table called
table
- or maybe I got this wrong.If you intend to use HABTM you do not need to create the join model at all - cake will automatically create and populate it for you.Some more info for HATBM: