HasMany通过多个条目

我正在通过CakePHP菜谱中的CourseMembership HasMany玩弄示例,但我无法弄清楚如何在同一时间向CourseMembership(即student_id和grade)添加一个新课程和多个条目。

课程有许多课程资料
学生有许多课程会员
课程属于学生,课程

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

数据数组成功保存,插入新的课程罚款,但只插入1个没有student_id或成绩的Coursemembers条目。

数据数组看起来像:

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


从表单输入中删除Coursemembership ---使用此

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

得到它了! 我按照建议使用课程控制器,并使用以下课程/添加视图,现在可以使用。 谢谢

    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');
链接地址: http://www.djcxy.com/p/64889.html

上一篇: HasMany Through multiple entries

下一篇: How do I traverse CakePHP relations?