【问题标题】:HasMany Through multiple entries - newHasMany 通过多个条目 - 新
【发布时间】:2014-03-26 12:29:10
【问题描述】:

我正在通过 CakePHP 食谱中的示例使用 CourseMembership HasMany,但我不知道如何同时将新课程和多个条目添加到 CourseMembership(即 student_id 和等级)中。

课程有许多课程会员
学生有很多课程会员
Coursemembership 属于 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');
?>

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

数据数组如下:

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

)

【问题讨论】:

    标签: cakephp associations has-many-through has-many


    【解决方案1】:

    从表单输入中删除 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');
    

    【讨论】:

    • 这个似乎也不起作用 - 课程已创建,但只有一个条目保存在 Coursemembership 中,没有 student_id 也没有成绩。
    • 你说好像???你试过吗?首先,如果你使用 CoursesController 这将是小菜一碟...我不明白你为什么要创建一个额外的控制器!!!其次,你的数组看起来像 `array('Course' => array('name' => ''), 'Coursemembership' => array( (int) 0 => array('grade' => '') , (int) 1 => 数组( 'grade' => '' ) ) ) `
    • 是的,我试过了。我使用了 Coursemembership 控制器,因为这是文档中使用的类似示例(book.cakephp.org/2.0/en/models/saving-your-data.html - 在保存 hasMany 下)。如果我在 Course 控制器中执行此操作,视图会是什么样子?
    【解决方案2】:

    知道了!我按照推荐使用了 Course 控制器,并带有以下 Course/add 视图,现在它可以工作了。谢谢

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

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2015-05-23
      • 2015-04-16
      • 2018-08-20
      • 1970-01-01
      相关资源
      最近更新 更多