2

我有三个表,我会在数据库中插入一个数组。它的一个电视节目,有很多季节。季节有很多情节。这是我的表:

节目

id
title
created
modified

季节

id
shows_id
created
modified

剧集

id
seasons_id
title
created
modified

模型文件:

显示.php

<?php
class Show extends AppModel {
    public $name = 'Show';
    public $hasMany = 'Season';
}

季节.php

<?php
class Season extends AppModel {
    public $name        = 'Season';
    public $belongsTo   = 'Show';
    public $hasMany     = '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);

我怎样才能插入这个数组?

问候

4

1 回答 1

2

您可以尝试以下代码来保存多个关联数据:

 $this->Show->save($sql_show, array('deep' => true));

您也可以使用saveAssociated()方法进行尝试。

请询问它是否不适合您。

于 2012-08-07T11:52:29.317 回答