0

我目前正在使用 CakePHP 2.0-RC1。非常漂亮,我只遇到了一个我无法解决的挑战。我已经阅读了有关将模型链接在一起的文档(http://www.cakedocs.com/models/associations-linking-models-together.html),但是如何告诉 CakePHP 通过关系表搜索并找到值我追求?让我解释。

我有一个类似于以下的数据库结构(已针对问题进行了简化。PK = Primary Key。FK = Foreign Key)

游戏

 - INT(11) id (PK)
 - VARCHAR(100) name
 - VARCHAR(40) year
 - VARCHAR(10) age

类别

 - INT(11) id (PK)
 - VARCHAR(50) name

游戏类别

 - INT(11) game_id (FK)
 - INT(11) category_id (FK)

解释这些之间的关系:

一个游戏可以有一个或多个类别。这种关系在“game_category”表中定义。我希望 CakePHP 不仅能找到游戏的类别 ID,我还希望在执行 $this->Game->find('first') 时也能找到类别名称。我猜想 CakePHP 需要被告知“继续遍历 game_category 表并进入类别表”并找到每个类别的实际名称。

我有这些模型

游戏.php

<?php
class Game extends AppModel {
    var $useTable = 'game';

    var $hasMany = array(
    'GameCategory' => array(
        'fields' => '*',
        'className' => 'GameCategory',
        )
    );

    var $hasOne = 'Review';  
}
?>

游戏分类.php

<?php
class GameCategory extends AppModel {
    var $useTable = 'game_category';

    var $belongsTo = 'category';

    var $hasMany = array(
    'Category' => array(
        'fields' => '*',
        'foreignKey' => 'category_id',
        'className' => 'Category',
        'conditions' => array('GameCategory.game_id' => 'Game.id', 'GameCategory.category_id' => 'Category.id'),
    )
    );
}
?>

分类.php

<?php
class Category extends AppModel {
    var $useTable = 'category';   
    var $belongsTo = 'GameCategory';
}
?>

通过上面定义的关系,我得到如下结果:

Array
(
    [Game] => Array
        (
            [id] => 2
            [name] => Colonization
            [year] => 1995
            [age] => 7
        )

    [GameCategory] => Array
        (
            [0] => Array
                (
                    [game_id] => 2
                    [category_id] => 19
                )

            [1] => Array
                (
                    [game_id] => 2
                    [category_id] => 16
                )

        )
)

剩下的唯一事情就是获取每个类别的实际名称。我希望其中一些有意义。

任何帮助表示赞赏。

4

2 回答 2

1

您应该在在线书籍中看到hasAndBelongsToMany (HABTM),并且应该将表名 game_category 更改为 games_categories 以获取正确的约定。

游戏.php

<?php
class Game extends AppModel {
    var $name = 'Game';
    var $hasAndBelongsToMany = array(
    'Category' =>
        array(
            'className'              => 'Category',
            'joinTable'              => 'game_category',
            'foreignKey'             => 'game_id',
            'associationForeignKey'  => 'category_id',
        )
);

} ?>

于 2011-09-25T16:43:24.123 回答
0

所有表名必须以复数形式(游戏,类别)
错误的类别关联,必须 hasAndBelongsToMany
使用蛋糕控制台( ./cake bake )以防止将来出现此类错误

<?php
class Category extends AppModel {
    var $name = 'Category';
    var $displayField = 'name';
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $hasAndBelongsToMany = array(
        'Game' => array(
            'className' => 'Game',
            'joinTable' => 'game_categories',
            'foreignKey' => 'category_id',
            'associationForeignKey' => 'game_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

} 
<?php
class Game extends AppModel {
    var $name = 'Game';
    var $displayField = 'name';
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    var $hasMany = array(
        'GameCategory' => array(
            'className' => 'GameCategory',
            'foreignKey' => 'game_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

#games
$games = $this->Game->Category->find('all');
于 2011-09-25T16:49:34.997 回答