Cakephp 设置自定义$this->;请求->;不破坏表单其余部分的数据?

Cakephp 设置自定义$this->;请求->;不破坏表单其余部分的数据?,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我有一个课程的add()视图,其中有一个表单: <?php echo $this->Form->create('Lesson'); ?> <fieldset> <legend><?php echo __('Add Lesson'); ?></legend> <?php echo $this->Form->input('name'); echo

我有一个课程的add()视图,其中有一个表单:

<?php echo $this->Form->create('Lesson'); ?>
    <fieldset>
        <legend><?php echo __('Add Lesson'); ?></legend>
    <?php
        echo $this->Form->input('name');
        echo $this->Form->input('course_id', array('selected' => $this->request->data['named']['belongsToCourse']));
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
下面是add()操作:


首先,课程表的课程id在数据库中是否设置为唯一?其次,使用表单显示视图的操作?@Nunser course\u id似乎没有设置为unique。表单视图的操作被添加到问题中。您在那里所做的是非常非常规的。您不应该像那样处理请求数据。坚持惯例和书本/教程教给你的东西。此外,您不应使用“选定”作为默认值。它打破了形式。请看。@mark抱歉,我对cakephp比较陌生。您的意思是通过模型传递参数吗?谢谢你的更新。我去看看。你可以使用
Form::hidden()
value
属性保存它
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'name'
public function add() {
        $this->request->data = $this->request->params;
        if ($this->request->is('post')) {
            $courseID = $this->request->data['named']['belongsToCourse'];
            $this->Lesson->create();
            if ($this->Lesson->save($this->request->data)) {
                $this->Session->setFlash(__('The lesson has been saved.'));
                return $this->redirect(array('controller' => 'courses', 'action' => 'view', $courseID));
            } else {
                $this->Session->setFlash(__('The lesson could not be saved. Please, try again.'));
            }
        }
        //debug(func_get_args());
        $courses = $this->Lesson->Course->find('list');
        $this->set(compact('courses'));

        //$this->Form->input('course_id') = $belongsToCourse;
}