在CakePHP中与一个表的多个关系 [英] Multiple relations to one table in CakePHP

查看:128
本文介绍了在CakePHP中与一个表的多个关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程式中,使用者可以是其他使用者的好友。所以他们有两个表之间的多对多关系。以这种方式: Users>朋友<用户

In my app a user can be friends with another user. So they have a many to many relationship between two tables. In this fashion: Users > Friends < Users

表格如下:

Users:
id
username
password

Friends:
id
user_id_from
user_id_to

我已将模型设置为:

class User extends AppModel
{
    public $name = 'User';

    public $hasOne = 'Profile';

    public $hasMany = array(
        'Post',
        'Answer',
    'Friend'
    );

    public $actsAs = array('Containable');
}

class Friend extends AppModel
{
    var $name = 'Friend';

    var $belongsTo = array(
        'Sender' => array(
            'className' => 'User',
            'foreignKey' => 'user_id_from'
        ),
        'Recipient' => array(
            'className' => 'User',
            'foreignKey' => 'user_id_to'
        )
    );

    public $actsAs = array('Containable');
}

但是似乎没有正确链接?

However it doesn't seem to link them properly?

我在用户模型中使用这个代码:

I use this code inside the User Model:

public function getFriends($username)
    {
        return $this->find('all', array('conditions' => array('User.username' => $username, 'Friend.status'=>1)

        ));
    }

,然后访问 USERNAME / friends 它应该列出该人的朋友。调用它的方法是:

and then when visiting a url like /users/USERNAME/friends it should list the friends for that person. The method that calls it is:

public function index( $username )
{   
    $friends = $this->User->getFriends($username);

    $this->set('friends', $this->paginate());
}

有什么问题?

推荐答案

对于朋友关系,您应该使用HABTM而不是hasMany。

For a friend relationship, you should use HABTM instead of hasMany.

class User extends AppModel
{
    public $name = 'User';

    public $hasOne = 'Profile';

    public $hasMany = array(
        'Post',
        'Answer',
    );

    public $hasAndBelongsToMany = array(
      'Friend' => array(
        'className' => 'User',
        'joinTable' => 'friends',
        'foreignKey' => 'user_id_to',
        'associationForeignKey' => 'user_id_from',
      )
    );

    public $actsAs = array('Containable');
}

删除您当前的Friend模型,因为它不需要,可能会混淆Cake。

Remove your current Friend model as it is not needed and will possibly confuse Cake.

您的 getFriends()方法将如下所示:

public function getFriends($username) {
   return $this->find('all', array(
     'conditions' => array(
       'User.username' => $username, 
       'User.status'=>1
      ),
      'contain' => array(
        'Friend'
      )
   ));
}

在这里,我们搜索User模型并包括所有该用户的Friend记录。 Cake将自动获取连接表数据并包括用户数据。 $ this-> User-> getFriends('Jeremy'); 将返回如下所示的数据:

Here, we search on the User model and include all that user's Friend records. Cake will automatically get the join table data and include the User data. $this->User->getFriends('Jeremy'); will return data that looks like this:

Array
(
    [User] => Array
        (
            [id] => 1
            [username] => Jeremy
        )
    [Friend] => Array
        (
           [0] => Array
                (
                    [id] => 2
                    [username] => Cameron
                )
           [1] => Array
                (
                    [id] => 3
                    [name] => Mark
                )
           [2] => Array
                (
                    [id] => 4
                    [name] => Bob
                )
        )
)

用户模型结果。

您可以在这里阅读有关HABTM的更多信息: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

You can read more about HABTM here: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

这篇关于在CakePHP中与一个表的多个关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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