CakePHP模型链接,belongsTo,hasOne

时间:2013-05-30 09:30:58

标签: database cakephp model foreign-keys cakephp-2.0

我看了the CakePHP book但是无法做到 我有一个名为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表链接到个人资料表。这是一对一(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] => 
             )

    )

我确信个人资料表有一个profile_user_id = 9200的记录。但是个人档案记录是空的。

1 个答案:

答案 0 :(得分:0)

我有点困惑为什么要将好友列表与个人资料相关联。将人与人联系起来然后从中得到简介会不会更有意义吗?

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

因此,在您的Person模型中,您要指定他们与其他人(或您的个人资料中)相关联,并指定查找表。

像...一样的东西。

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

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

类似的东西:

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

您可能需要调整名称,因为我很难准确地消化实体正在播放的内容,但至少应该给您一个想法。

相关问题