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

发布于 2024-12-06 23:07:25 字数 2463 浏览 0 评论 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)

对它们之间关系的解释

一款游戏可以有一个或多个类别。该关系在“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
                )

        )
)

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

任何帮助表示赞赏。

I'm currently using CakePHP 2.0-RC1. Being pretty nifty and all, I've only come across one challenge that I can't wrap my mind around. I've read the documentation on linking models together (http://www.cakedocs.com/models/associations-linking-models-together.html), but how to I tell CakePHP to search trough a relation table and find the values I'm after? Let me explain.

I have a database structure similar to the following (it has been simplified for the question. PK = Primary Key. FK = Foreign Key )

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:

A game can have one or more category. This relation is defined in "game_category" table. I want CakePHP to not only find what category id a game has, I want the category name as well when I do $this->Game->find('first') for instance. I would guess CakePHP needs to be told to "continue through the game_category table and onto the category table" and find what the name of each category actually is.

I have these Models

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';
}
?>

By the relations defined above, I get results like this:

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
                )

        )
)

The only thing remaining is just getting the actual name of each category. I hope some of this made any sense.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-12-13 23:07:25

您应该看到 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',
        )
);

}
?>

You should see hasAndBelongsToMany (HABTM) in book online and should change table name game_category to games_categories for correct conventions.

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

}
?>

完美的未来在梦里 2024-12-13 23:07:25

所有表格名称必须为复数(游戏、类别)
错误的类别关联,必须 hasAndBelongsToMany
使用蛋糕控制台( ./cake cake )来防止将来出现这样的错误

<?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');

all table names must in plural ( games, categories )
wrong category association, must hasAndBelongsToMany
use cake console ( ./cake bake ) to prevent future errors like this

<?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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文