首页 文章

CakePHP - 如何将这些模型链接在一起(需要使用关系表)?

提问于
浏览
0

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

我有一个类似于以下的数据库结构(它已被简化为问题.PK =主键.FK =外键)

game

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

category

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

game_category

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

Explanation to the relationship between these:

游戏可以有一个或多个类别 . 该关系在“game_category”表中定义 . 我希望CakePHP不仅可以找到游戏中的类别ID,我还需要类别名称,例如当我执行$ this-> Game-> find('first')时 . 我猜想CakePHP需要被告知“继续通过game_category表并进入类别表”并找到每个类别的名称实际上是什么 .

我有这些模特

Game.php

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

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

    var $hasOne = 'Review';  
}
?>

GameCategory.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'),
    )
    );
}
?>

Category.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
                )

        )
)

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

任何帮助表示赞赏 .

2 回答

  • 1

    所有表名必须为复数(游戏,类别)
    错误的类别关联,必须具有AndBelongsToMany
    使用cake console(./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');
    
  • 0

    你应该在网上看到hasAndBelongsToMany (HABTM),并且应该将表名game_category更改为games_categories以获得正确的约定 .

    Game.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',
            )
    );
    

    }?>

相关问题