我有三个表,我会将一个数组插入数据库。它的一个电视节目,有很多季节。季节有很多剧集。这是我的表格:
:显示
id
title
created
modified
季节
id
shows_id
created
modified
集
id
seasons_id
title
created
modified
模型文件:
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';
}
我用这个数组尝试过它:
$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);
如何插入此数组?
问候
答案 0 :(得分:2)
您可以尝试以下代码来保存多个关联数据:
$this->Show->save($sql_show, array('deep' => true));
您也可以使用saveAssociated()方法尝试。
请问它是否对你不起作用。