cakephp friendship between users Linking models to

2019-09-21 19:52发布

问题:

What I need:

A facebook-like friendship system.

  1. User (A) sends a requests for a friendship with User (B)
  2. User (B) confirmes the request
  3. User (A) and User (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' => ''
                );

回答1:

"@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 in config.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).

What to do when HABTM becomes complicated?

By default when saving a HasAndBelongsToMany relationship, Cake will delete all rows on the join table before saving new ones. For example if you have a Club that has 10 Children associated. You then update the Club with 2 children. The Club will only have 2 Children, not 12. Also note that if you want to add more fields to the join (when it was created or meta information) this is possible with HABTM join tables, but it is important to understand that you have an easy option. HasAndBelongsToMany between two models is in reality shorthand for three models associated through both a hasMany and a belongsTo association.

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:

  1. Here
  2. There
  3. Here
  4. There