用户之间的cakephp友谊将模型链接在一起 [英] cakephp friendship between users Linking models together

查看:168
本文介绍了用户之间的cakephp友谊将模型链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么:

类似于Facebook的友谊系统。

A facebook-like friendship system.


  1. 用户(A)发送与<$ c $的友谊请求c>用户(B)

  2. 用户(B)确认请求

  3. 用户(A)用户(B)现在是朋友

  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

我的问题

我很困惑如何解决这个问题。我在互联网上阅读了很多东西,但并没有真正帮助我...

I'm confused how to work this out. I read a lot on the internet but it did not really helped me...

我的问题:


  • 在CakePHP中是什么样的链接?它有AndAndlongsToMany吗?还是有很多?还是...?

  • 如何在日期数据库中正确实现它?

  • 如何在模型中链接它?

我已经做过的事情:

表格:用户

名称: id 用户名密码,...

users_users表: id user_id friend_id 已批准

users_users table: id, user_id, friend_id, approved

型号:

'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谢谢!但是我得到了一个错误:错误:发生内部错误

"@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:

尝试在 config.php 中将 debug 设置为2,您将意识到您将开始得到更多可理解的错误。 。

Try setting debug to 2 in config.php you will realize that you will start getting much more understandable errors.

关于第一个问题:
1.您的关系基本上看起来像 hasMAny ,因为每个用户都有朋友。
HABTM也可以在这里使用,但是要复杂得多。这个决定(要使用什么关系)还取决于系统的其他部分-即您要在什么数据中以及如何在其他部分中使用此数据。
2.阅读此处
3.再次阅读再次出现

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

有关 HABTM 通过jon模型已经很多(如果您需要在使用与HABTM相同的思想的同时在连接表中存储任何其他数据)。

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).


当HABTM变得复杂时该怎么办?

保存HasAndBelongsToMany关系时的默认设置,Cake将在savin之前删除联接表上的所有行
g个新的。例如,如果您有一个包含10个子级的俱乐部。然后,您更新有2个孩子的俱乐部。俱乐部将只有2个孩子,而不是12个。
另外请注意,如果要向联接中添加更多字段(在创建联接或元信息时),可以通过HABTM联接表来实现,但对于了解您有一个简单的选择。实际上,两个模型之间的
HasAndBelongsToMany是通过hasMany和alongToTo
关联而关联的三个模型的简写。

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.

您当前的数据库结构很糟糕。我不明白为什么您需要一个名为 table 的表-否则我可能会错了,如果您打算使用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.

HATBM的更多信息:

Some more info for HATBM:


  1. 此处

  2. 在那里

  3. 此处

  4. 在那里

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

这篇关于用户之间的cakephp友谊将模型链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆