简体   繁体   中英

CakePHP add model association into database table

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!

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM