私はGameというモデルを持っています。これは、PlanetモデルとhasManyの関係があります(このモデルには、Gameモデルに定義されたbelongsToがあります)。
最初のゲームレコードを作成するときに、starting_num_planetsフィールドの値を使用して、関連する多数のPlanetレコードを作成しようとします。GamesControllerは、PlanetsControllerの関数を呼び出すことによってこれを行います。この関数は、呼び出されるたびに、Planetモデルに一致する配列を返し、保存可能である必要があります。これは、GamesControllerが次に実行しようとすることです。
ゲームレコードは問題なく作成されますが、2番目のコントローラーの機能にアクセスしようとしている時点で、どういうわけか無効なSQL構文エラーが発生しています。数行後まで保存しようとしないので、これは奇妙です。
GamesControllerのコードは次のとおりです。
public function newgame() {
if ($this->request->is('post')) {
$this->Game->create();
if ($this->Game->save($this->request->data)) {
// Add the 'id' element to the Game array and put the whole game record on the session
$this->request->data['Game']['id'] = $this->Game->getLastInsertID();
$this->Session->write('Game', $this->request->data);
// Create the number of planet records indicated by start_num_planets
$count = 0;
$planets = array();
while ($count < $this->request->data['Game']['starting_num_planets']) {
$planetData = $this->Game->Planet->generate_planet_data($this->request->data['Game']['id']);
$planets[$count] = $planetData;
$this->Game->Planet->create();
$this->Game->Planet->save($planetData);
$count++;
}
$this->redirect(array('action' => 'main'));
} else {
$this->Session->setFlash('There was a problem creating the game.');
}
}
}
この行の行番号:
$ planetData = $ this-> Game-> Planet-> generate_planet_data($ this-> request-> data ['Game'] ['id']);
私は得ています:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generate_planet_data' at line 1
SQL Query: generate_planet_data
そして、私は理由がわかりません、私は恐れています。
PlanetsControllerで呼び出される関数は次のとおりです。
public function generate_planet_data($gameId) {
$planetNames = array(1 => 'name1', 2 => 'name2', 3 => 'name3', 4 => 'name4');
$planetImages = array(1 => '01', 2 => '02', 3 => '03', 4 => '04');
$planetData = array();
// game_id is the foreign key, do I have to explicitly state that in the model?
$planetData['Planet']['game_id'] = $gameId;
$planetData['Planet']['planet_name'] = $planetNames[rand(1,10)];
$planetData['Planet']['planet_image_file'] = $planetImages[rand(1,4)];
return $planetData;
}
Game.phpのモデルの関連付けは単純です。
var $hasMany = array(
'Planet' => array(
'className' => 'Planet'),
'Player' => array(
'className' => 'Player'
)
);
誰かが何が悪いのかわかりますか?これは、関連するモデルを使用する最初の試みであり、少しうまくいきませんでした。:)