CakePHP - 如何将这些模型链接在一起(需要使用关系表)?
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该看到 hasAndBelongsToMany (HABTM) 在在线书籍中,应将表名 game_category 更改为 games_categories 以确保正确的约定。
Game.php
}
?>
You should see hasAndBelongsToMany (HABTM) in book online and should change table name game_category to games_categories for correct conventions.
Game.php
}
?>
所有表格名称必须为复数(游戏、类别)
错误的类别关联,必须 hasAndBelongsToMany
使用蛋糕控制台( ./cake cake )来防止将来出现这样的错误
all table names must in plural ( games, categories )
wrong category association, must hasAndBelongsToMany
use cake console ( ./cake bake ) to prevent future errors like this