0

I am playing around with the CourseMembership HasMany through example in the CakePHP cookbook but I cant figure out how to add a new Course and multiple entries into CourseMembership (i.e. student_id and grade) all at the same time.

Course hasMany Coursemmembership
Student hasMany Coursemeembership
Coursemembership belongsTo Student, Course

//CoursemmembershipsController
public function add() {
    if ($this->request->is('post')) {
        $this->Coursemembership->create();
        if ($this->Coursemembership->saveAll($this->request->data,array('deep' => true))) {
            $this->Session->setFlash(__('The coursemembership has been saved.'));
            //return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The coursemembership could not be saved. Please, try again.'));
        }
        debug($this->request->data);
    }
    $courses = $this->Coursemembership->Course->find('list');
    $students = $this->Coursemembership->Student->find('list');
    $this->set(compact('courses', 'students'));
}

__

//Coursemembership/view/add
$this->Form->create('Coursemembership'); 


    echo $this->Form->input('Course.name');
    echo $this->Form->input('0.Coursemembership.student_id');
    echo $this->Form->input('0.Coursemembership.grade');

    echo $this->Form->input('1.Coursemembership.student_id');
    echo $this->Form->input('1.Coursemembership.grade');
?>

The data array successfully saves, inserts a new Course fine, but inserts only 1 Coursemembership entry with no student_id or grade.

Data array looks like:

array(
'Course' => array(
    'name' => 'Math 101'
),
(int) 0 => array(
    'Coursemembership' => array(
        'student_id' => '1',
        'grade' => '5'
    )
),
(int) 1 => array(
    'Coursemembership' => array(
        'student_id' => '2',
        'grade' => '2'
    )
)

)

2 Answers 2

0

remove Coursemembership from Form input --- Use this

    echo $this->Form->create('Coursemembership'); 
    echo $this->Form->input('Course.name');
    echo $this->Form->input('0.student_id');
    echo $this->Form->input('0.grade');

    echo $this->Form->input('1.student_id');
    echo $this->Form->input('1.grade');
    echo $this->Form->end('save');
3
  • This one didn't seem to work either - the course is created but only a single entry is being saved in Coursemembership, with no student_id and no grade.
    – Nate88
    Commented Mar 27, 2014 at 11:35
  • you said seems??? Did you tried that?? First of all, if you go with CoursesController this will be piece of cake...I don't understand why you created an extra Controller!!! Second, you array sould looks like that ` array( 'Course' => array( 'name' => '' ), 'Coursemembership' => array( (int) 0 => array( 'grade' => '' ), (int) 1 => array( 'grade' => '' ) ) ) ` Commented Mar 27, 2014 at 16:35
  • Yep I did try it. I used the Coursemembership controller as this is what a similar example in the documentation used (book.cakephp.org/2.0/en/models/saving-your-data.html - under saving hasMany through). What would the view look like like if I did it in the Course controller?
    – Nate88
    Commented Mar 29, 2014 at 3:26
0

Got it! I used the Course controller as recommended, with the following Course/add view and it now works. Thanks

    echo $this->Form->input('Course.name');
    echo $this->Form->input('Coursemembership.0.student_id');
    echo $this->Form->input('Coursemembership.0.grade');
    echo $this->Form->input('Coursemembership.1.student_id');
    echo $this->Form->input('Coursemembership.1.grade');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.