我目前使用CakePHP 2.0-RC1。虽然我很聪明,但我只遇到过一个让我无法思考的挑战。我已经阅读了将模型链接在一起的文档(http://www.cakedocs.com/models/associations-linking-models-together.html),但是我如何告诉CakePHP通过关系表搜索并找到我要的值?让我解释一下。
我有一个类似于下面的数据库结构(它已经简化了问题。PK =主键。FK =外键)
游戏 - INT(11) id (PK)
- VARCHAR(100) name
- VARCHAR(40) year
- VARCHAR(10) age
- 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
)
)
)
剩下的唯一事情就是获取每个类别的实际名称。我希望这些对你有意义。
您应该在book online中看到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',
)
);
}div ?>
所有表名必须为复数(游戏,类别)
类别关联错误,必须使用andbelongstomany
使用蛋糕控制台(./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');