CakePHP 将模型关联添加到数据库表中
我是 CakePHP 的新手,英语不是我的母语,所以如果我的问题不清楚,我深表歉意。不管怎样,我有两个模型:开发者和游戏。
<?php
class Developer extends AppModel
{
var $name = 'Developer';
var $belongsTo = array('Game');
}
?>
我怎样
<?php
class Game extends AppModel
{
var $name = 'Game';
var $hasMany = array('Developer');
}
?>
才能在表developer_games中添加一个新行,该表只有game_id和developer_id字段,表明游戏和开发者之间存在关系,而实际上不知道游戏和开发者的id,因为它们是在创建的同时。我认为 CakePHP 能够为我做到这一点,但它不会在developer_games 表中添加新行。保存数据后是否必须检索游戏和开发人员的“id”字段,然后手动将关系模型数据保存到developer_games表中?
这是我用来向数据库添加新游戏和开发人员的代码:
$data = $this->Game->saveAll(array(
'Game' => array(
'game_id' => $data['GameId'],
'game_name' => $data['GameName'],
),
'Developer' => array(
'Developer' => array(
'username' => $_POST['dev_username'],
'password_hash' => $_POST['dev_password'],
),
),
));
$this->Game->saveAll($data);
如果我不清楚某些内容,请告诉我,我会澄清。我已经为这个问题苦苦挣扎了很长一段时间,所以我将不胜感激我能得到的任何帮助。谢谢!
I'm new to CakePHP and English isn't my first language so I apologize if my question isn't clear. Anyway, I have two models: Developer and Game.
<?php
class Developer extends AppModel
{
var $name = 'Developer';
var $belongsTo = array('Game');
}
?>
and
<?php
class Game extends AppModel
{
var $name = 'Game';
var $hasMany = array('Developer');
}
?>
How would I be able to add a new row in the table developer_games that only has game_id and developer_id fields that indicates there is a relationship between a game and a developer without actually knowing the id's of the game and developer because they're created at the same time. I thought CakePHP was able to do this for me but it wouldn't add a new row in the developer_games table. Would I have to retrieve the 'id' fields of the Game and Developer after saving data then save the relationship model data to the developer_games table manually afterwards?
Here's the code I use to add a new game and developer to the database:
$data = $this->Game->saveAll(array(
'Game' => array(
'game_id' => $data['GameId'],
'game_name' => $data['GameName'],
),
'Developer' => array(
'Developer' => array(
'username' => $_POST['dev_username'],
'password_hash' => $_POST['dev_password'],
),
),
));
$this->Game->saveAll($data);
If I wasn't clear with something, let me know and I'll clarify. I've been struggling with this problem for a long time, so I'll appreciate any help I can get. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没理解错的话,逻辑图中游戏-开发者的关系是多对多的,那么应该是[游戏]-1:N-[分配]-N:1-[开发者]
这里分配的是你的“developer_games”表,我建议你重命名该表以方便
根据你的场景,推荐在CakePHP中实现3个模型类。
这是添加新分配的代码
If I dont misunderstood, Game-Developer relationships in many-to-many in logical diagram, then it should be [Game]-1:N-[Assignment]-N:1-[Developer]
Assignment here is your "developer_games" table, I suggest you rename the table for convenient
According to your scenario, recommended implementation in CakePHP would be 3 model classes.
Here is the code to add new Assignment
请阅读以下内容:http://book.cakephp.org/view/1044/ hasAndBelongsToMany-HABTM
“hasAndBelongsToMany”是您想要的关系。 Cake 负责关联,您不需要连接表的模型。
Please have a read on this: http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM
"hasAndBelongsToMany" is the relationship you want. Cake takes care about the associations and you don't need a model for the join table.