0

所以我正在建立一个课程系统,课程有前置要求。一门课程可能是许多其他课程的先决条件,并且本身也可能有许多先决条件。我有一个 Course 模型,我目前正在使用 HABTM 连接表,因为我还需要跟踪 pre-req 的类型(常规、co-req 或我们所说的“pre-req with concurrency”:你可以同时接受先决条件)。这是课程模型:

class Course extends AppModel {
        public $name = 'Course';
        public $belongsTo = 'Department';
        public $hasMany = array(
            'Instance' => array('className' => 'Instance'),
        );
        public $hasAndBelongsToMany = array(
            'Prereq' => array(
                'className' => 'Course',
                'joinTable' => 'prereq_successor',
                'foreignKey' => 'successor_id',
                'associationForeignKey' => 'prereq_id', 
                'unique' => 'keepExisting'
            )
        );
}

问题是我需要为 saveAll 构建一些数据(来自通过文件上传的数据,而不是表单)并且无法从文档中弄清楚如何做到这一点。

我已经尝试过这个(控制器代码),基于 cakePHP 2.0 书,但它默默地失败了:

//test based on cakePHP 2.0 book fails:
$data = array(
    'Course' => array('id' => 1),
    'Prereq' => array(
        'successor_id' => 1,
        'prereq_id' => 3,
        'type' => 'prereq'
    )
);
$result = $this->Course->saveAssociated($data);

我试过这个(控制器代码)但没有成功:

//test based on cakePHP 2.0 book fails:
$data = array(
    array(
        'Course' => array('id' => 1),
        'Prereq' => array(
            'successor_id' => 1,
            'prereq_id' => 3,
            'type' => 'prereq'
        )
    )
);
$result = $this->Course->saveAll($data);

在此先感谢,戴夫

4

1 回答 1

0

我认为问题出在您的数据上。尝试这个:

$data = array(
    array(
        'Course' => array('id' => 1),
        'Prereq' => array(
            array(
                'successor_id' => 1,
                'prereq_id' => 3,
                'type' => 'prereq'
            )
        )
    )
);
$result = $this->Course->saveAll($data);
于 2012-09-19T11:40:49.180 回答