0

So I am setting up a course system, and courses have pre-reqs. One course may be a pre-req for many others and also may itself have many pre-reqs. I have a Course model, and I'm currently using a HABTM join table, because I also need to track the type of pre-req (regular, co-req, or what we call "pre-req with concurrency": you can take the pre-req at the same time). Here's the Course model:

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'
            )
        );
}

The problem is that I need to construct some data for saveAll (from data uploaded via a file, not a form) and can't figure out how to do it from the docs.

I've tried this (controller code), based on the cakePHP 2.0 book, but it fails silently:

//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);

And I've tried this (controller code) without success:

//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);

Thanks in advance, Dave

Dave Hirsch
  • 197
  • 13

1 Answers1

0

I think the problem is with your data. Try this:

$data = array(
    array(
        'Course' => array('id' => 1),
        'Prereq' => array(
            array(
                'successor_id' => 1,
                'prereq_id' => 3,
                'type' => 'prereq'
            )
        )
    )
);
$result = $this->Course->saveAll($data);
fsilva
  • 71
  • 4