0

我读了 CakePHP 的书,但没能成功。
我有一张名为frendslists. 每个用户 ( owner_user_id) 的朋友太多,我将朋友添加到friend_id列中。(模型名称是 Friendslist)

CREATE TABLE IF NOT EXISTS `friendslists` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `owner_user_id` int(20) unsigned NOT NULL,
  `friend_id` int(20) NOT NULL COMMENT 'id of the friend',
  PRIMARY KEY (`id`),
  KEY `friend_id` (`friend_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--id-------owner_user_id-----friend_id
--------------------------------------
--1--------1234--------------9200-----
--2--------1234--------------3210-----
--3--------1234--------------7600-----

我也有一张profiles桌子。每个独特的人在那里都有一个个人资料。一个人只能拥有一份个人资料。(型号名称为 Profile)

CREATE TABLE IF NOT EXISTS `profiles` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `profile_user_id` int(20) NOT NULL,
  `name` varchar(50) NOT NULL,
  `location` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `profile_user_id` (`profile_user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--id------profile_user_id---name------location-
-----------------------------------------------
--1-------9200--------------michael----usa-----
--2-------3210--------------john-------uk------
--3-------7600--------------danny------denmark-

我想将friendslists 表链接到profiles 表。这是一对一(hasOne)还是多对一(belongsTo)类型的关系?

当我查询朋友列表表时,我想获取朋友的个人资料数据。我应该在 CakePHP 模型和表格中做什么?

我创建了一个这样的外键:

ALTER TABLE `friendslists`
  ADD CONSTRAINT `friendslists_ibfk_1` 
  FOREIGN KEY (`friend_id`) 
  REFERENCES `profiles` (`profile_user_id`) 
  ON DELETE CASCADE ON UPDATE CASCADE;

我将模型文件更改为:

class Friendslist extends AppModel {
    var $name = 'Friendslist';
    var $useTable = 'friendslists';

    public $belongsTo = array(
        'Profile' => array(
            'className'    => 'Profile',
            'foreignKey'   => 'friend_id'
        )
    )

    function getAll(){
        return $this->find('all');
    }
}

最后当我这样做时:

$records=$this->Friendslist->find('all', array('conditions' => array(
    'Friendslist.owner_user_id' => 1234)
));

我得到这些结果:

     [Friendslist] => Array
            (
                [id] => 1
                [owner_user_id] => 1234
                [friend_id] => 9200
            )

        [Profile] => Array
            (
                [id] => 
                [profile_user_id] => 
                [name] => 
                [location] => 
             )

    )

我确定profiles 表有一条profile_user_id=9200 的记录。但是个人资料记录是空的。

4

1 回答 1

0

我有点困惑为什么您要将好友列表链接到个人资料。将人与人联系起来然后从中获取个人资料不是更有意义吗?

无论如何,您所描述的是 HasAndBelongsToMany(HABTM)

因此,在您的 Person 模型中,您要指定他们有许多其他人(或在您的情况下为 Profiles)与他们关联并指定查找表。

就像是...

public $hasAndBelongsToMany = array(
        'Person' => array(
            'className' => 'Profile',
            'joinTable' => 'friendslists',
            'foreignKey' => 'owner_id',
            'associationForeignKey' => 'friend_id'
        )
    );

然后在friendslists 模型中,我将其描述为belongsTo 列出所有者和个人资料。

就像是:

public $belongsTo = array(
    'Person' => array(
            'className' => 'Person'
           ,'foreignKey' => 'owner_user_id'
        ),
    'Profile' => array(
            'className' => 'Profile'
           ,'foreignKey' => 'friend_id'
        )
);

您可能需要调整名称,因为我很难准确地消化哪些实体在起作用,但这至少应该让您有所了解。

于 2013-10-08T16:45:04.970 回答