i have three tables an i would insert one array to the database. Its one tv show, with many seasons. And the Seasons have many Episodes. Here are my tables:
shows
id
title
created
modified
seasons
id
shows_id
created
modified
episodes
id
seasons_id
title
created
modified
The Model files:
Show.php
<?php
class Show extends AppModel {
public $name = 'Show';
public $hasMany = 'Season';
}
Season.php
<?php
class Season extends AppModel {
public $name = 'Season';
public $belongsTo = 'Show';
public $hasMany = 'Episode';
}
Episode.php
<?php
class Episode extends AppModel {
public $name = 'Episode';
public $belongsTo = 'Season';
}
I had tried it with this Array:
$this->Show->create();
$sql_show = array(
'Show' => array(
'id' => 2,
'title' => 'Super Mega Show',
),
'Season' => array(
array(
'id' => 1,
'shows_id' => 2,
'Episode' => array(
array(
'id' => 1,
'title' => 'Episode Title 1'
),
array(
'id' => 2,
'title' => 'Episode Title 2 '
),
array(
'id' => 3,
'title' => 'Episode Title 3'
),
)
),
)
);
$this->Show->saveAll($sql_show);
How can i insert this array?
greetings
You can try the following code to save multiple associated data:
You can also try it using saveAssociated() method.
kindly ask if it not worked for you.