我想为新记录预填充一些字段值。我可以在下面看到硬代码,但我不能从$ lesson数组中提取它们,当我在视图中设置变量时,我无法从隐藏字段中获取它们。 在find中我得到了我需要的值,那么我用什么方法从find中提取变量?
http://book.cakephp.org/2.0/en/controllers/request-response.html
$lesson=$this->set( 'lesson',$this->Lesson->find('first',$options));
if ($this->request->is('post')) {
$this->TrequestAmend->create();
$this->request->data['TrequestAmend']['lesson_id']=6; //I want o set this value to lesson_id found in find first above but how???
$this->request->data['TrequestAmend']['tutor_id']=2;
$this->request->data['TrequestAmend']['student_id']=2;
if ($this->TrequestAmend->save($this->request->data)) {
$this->Session->setFlash(__('Your Tutor Requested Amended data has been saved.'));
// return $this->redirect(array('action' => 'displayall'));
}
else
$this->Session->setFlash(__('Unable to add your post.'));
}
// view
echo $this->Form->input($lessonId, array('type' => 'hidden'));
echo $this->Form->input($tutorId, array('type' => 'hidden'));
echo $this->Form->input($stId, array('type' => 'hidden'));
答案 0 :(得分:1)
$lesson = $this->Lesson->find('first', $options);
$this->request->data['TrequestAmend']['lesson_id'] = $lesson['Lesson']['id'];
$this->set(compact('lesson'));
答案 1 :(得分:0)
如果将值设置为模型$this->request->data
的{{1}},则必须确保为此模型创建了表单(我写这个是因为您没有在此处发布此行)。
TrequestAmend
然后,在为其名称创建表单输入时,必须在视图中更改变量。所以这样做:
echo $this->Form->create('TrequestAmend');
就是这样,现在你应该能够预先填写表格了。