【问题标题】:CakePHP model linking, belongsTo, hasOneCakePHP 模型链接,belongsTo,hasOne
【发布时间】:2013-05-30 09:30:58
【问题描述】:

我读了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 表链接到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 的记录。但是个人资料记录是空的。

【问题讨论】:

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


    【解决方案1】:

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

    无论如何,您所描述的是 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'
            )
    );
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      相关资源
      最近更新 更多