代码之家  ›  专栏  ›  技术社区  ›  Harsha M V

CakePHP 1.3.4$belongsTo关系表中的问题

  •  1
  • Harsha M V  · 技术社区  · 14 年前

    CREATE TABLE `user_relationships` (
      `id` int(11) unsigned NOT NULL auto_increment,
      `status` varchar(255) default 'pending',
      `time` datetime default NULL,
      `user_id` int(11) unsigned NOT NULL,
      `friend_id` int(11) unsigned NOT NULL,
      PRIMARY KEY  (`id`),
      KEY `fk_user_relationships_users1` (`user_id`),
      KEY `fk_user_relationships_users2` (`friend_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    

    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Friend' => array(
            'className' => 'Friend',
            'foreignKey' => 'friend_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    

    在这部分代码中,我想将friend\u id引用到User表

        'Friend' => array(
            'className' => 'Friend',
            'foreignKey' => 'friend_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    

        'Friend' => array(
            'className' => 'Friend',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    
    3 回复  |  直到 14 年前
        1
  •  2
  •   deceze    14 年前

    这个怎么样?

    class UserRelationship {
    
        var $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id'
            ),
            'Friend' => array(
                'className' => 'User',
                'foreignKey' => 'friend_id'
            )
        );
    
    }
    
    class User {
    
        var $hasAndBelongsToMany = array(
            'Friend' => array(
                'className' => 'User',
                'joinTable' => 'user_relationships',
                'foreignKey' => 'user_id',
                'associationForeignKey' => 'friend_id',
                'with' => 'UserRelationship'
             )
        );
    
    }
    

    从不同的角度可以看到关联:

    User        <-    Relationship    ->     User
     |                     |                  |
     hasMany           belongsTo              |
     Relationship  <-  User (user_id)        hasMany
     |                 User (friend_id)  ->  Relationship
     |                                        |
     HABTM                                   HABTM
     User       <---------------------->     User
    

    您的“物理”布局(即数据库模式)是 User -> Relationship -> User User -> User User hasAndBelongsToMany User 关联(也称为多对多)。由于它使用三种模型,因此多对多关联可以分解为:

    • 用户有许多关系/属于用户的关系
    • 关系属于用户/用户有许多关系

    你不知道 hasAndBelongsToMany协会,但这是你真正想要的。您根本不需要关系模型,因为它只是连接两个用户的助手模型,但是如果您

        2
  •  1
  •   Tim Falk    14 年前

    你的“朋友”的类名是错误的。因为您没有表“friends”,所以没有模型“friends”,并且您有引用类“User”的权限,因为它们共享同一个表。

    'Friend' => array(
        'className' => 'User',
        'foreignKey' => 'friend_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
    
        3
  •  1
  •   mmakowski    14 年前

    class User extends AppModel {
        var $hasAndBelongsToMany = array(
            'Friends1' => array(
                'className' => 'User',
                'joinTable' => 'friends',
                'foreignKey' => 'user_id',
                'associationForeignKey' => 'friend_id',
                'with' => 'Friend'
             ),
            'Friends2' => array(
                'className' => 'User',
                'joinTable' => 'friends',
                'foreignKey' => 'friend_id',
                'associationForeignKey' => 'user_id',
                'with' => 'Friend'
             )
        );
    
        function afterFind(array $results) {
    
            // Merge both sides of the relationship into one alias
            foreach ($results as $key=>$result) {
                if (isset($result["Friends1"]) && isset($result["Friends2"])) {
                    $results[$key]["Friends"] = array_merge($result["Friends1"], $result["Friends2"]);
                    unset($results[$key]["Friends1"]);
                    unset($results[$key]["Friends2"]);
                }
            }
    

    这很混乱,但你得到了这样一个想法:抓住关系的双方,然后在数据库读取后合并它们:)