CakePHP 将模型关联添加到数据库表中

发布于 2024-10-28 02:10:06 字数 1110 浏览 1 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(2

流心雨 2024-11-04 02:10:06

如果我没理解错的话,逻辑图中游戏-开发者的关系是多对多的,那么应该是[游戏]-1:N-[分配]-N:1-[开发者]
这里分配的是你的“developer_games”表,我建议你重命名该表以方便

根据你的场景,推荐在CakePHP中实现3个模型类。

<?php
class Developer extends AppModel
{
    var $name = 'Developer';
    var $hasMany = array('Assignment');
}
?>

<?php
class Game extends AppModel
{
    var $name = 'Game';
    var $hasMany = array('Assignment');
}
?>

<?php
class Assignment extends AppModel
{
    var $name = 'Assignment';
    var $belongsTo = array('Game','Developer');
}
?>

这是添加新分配的代码

//retrieved submitted data --> $data
$data = $this->data;

// this is for the case you want to insert into 3 tables at a same time
$newGame = $this->Game->create();
$newGame = array(
    'Game'=> array(
        'name' => $data['Game']['name']
    )
);
$this->Game->save($newGame);
$newGameId = $this->Game->getLastInsertId();

$newDev = $this->Developer->create();
$newDev = array(
    'Developer'=> array(
        'name' => $data['Developer']['name']
    )
);
$this->Developer->save($newDev);
$newDevId = $this->Developer->getLastInsertId();

/**
* or if you already have Game Id, and Developer Id, then just load it on, and 
* put it in the statement below, to create a new Assignment
**/
$newAssignment = $this->Assignment->create();
$newAssignment = array(
    'Assignment' => array(
        'game_id' => $newGameId,
        'developer_id' => $newDevId,
    )
);
$this->Assignment->save($newAssignment);

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.

<?php
class Developer extends AppModel
{
    var $name = 'Developer';
    var $hasMany = array('Assignment');
}
?>

<?php
class Game extends AppModel
{
    var $name = 'Game';
    var $hasMany = array('Assignment');
}
?>

<?php
class Assignment extends AppModel
{
    var $name = 'Assignment';
    var $belongsTo = array('Game','Developer');
}
?>

Here is the code to add new Assignment

//retrieved submitted data --> $data
$data = $this->data;

// this is for the case you want to insert into 3 tables at a same time
$newGame = $this->Game->create();
$newGame = array(
    'Game'=> array(
        'name' => $data['Game']['name']
    )
);
$this->Game->save($newGame);
$newGameId = $this->Game->getLastInsertId();

$newDev = $this->Developer->create();
$newDev = array(
    'Developer'=> array(
        'name' => $data['Developer']['name']
    )
);
$this->Developer->save($newDev);
$newDevId = $this->Developer->getLastInsertId();

/**
* or if you already have Game Id, and Developer Id, then just load it on, and 
* put it in the statement below, to create a new Assignment
**/
$newAssignment = $this->Assignment->create();
$newAssignment = array(
    'Assignment' => array(
        'game_id' => $newGameId,
        'developer_id' => $newDevId,
    )
);
$this->Assignment->save($newAssignment);
许仙没带伞 2024-11-04 02:10:06

请阅读以下内容: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.

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