我有一个场景,其中我有两个模型,profile和education,具有profile hasMany education关系。
然后我有一个视图,我想有多种形式:
- 用户配置文件的每个Education条目都有一个编辑表单
- 一个表单添加一个新条目
这里的问题是,我希望用每个条目的数据填充编辑表单。关于如何实现这一点,有什么想法吗
CakePHP文档指出:
FormHelper使用$this->request->data属性来自动检测是否创建添加或编辑表单。如果$this->request->data包含以表单模型命名的数组元素,并且该数组包含模型主键的非空值,则FormHelper将为该记录创建一个编辑表单
我已经成功地实现了一个编辑表单,但在我只有一个表单的情况下。
现在我有这样的东西:
EducationController.php
public class EducationController extends AppController{
/* (...) */
public function edit_index(){
$user_id = $this->Auth->user('id');
$profile = $this->Profile->find('first', array('conditions' => array('Profile.user_id' => $user_id)));
$this->set('profile', $profile);
$this->request->data = $profile;
}
/* (...) */
}
至于视图文件。。。
<?php
foreach ($profile['Education'] as $key => $value) {
?>
<div class="history-sub-menu" id="history-1">
<p> <?php echo $value['education'] . ' - ' . $value['school'] ?> </p>
<?php echo $this->Html->image("arrow_bot.png") ?>
</div>
<?php
/*
* Prints the form for editing... the id of the education entry being edited goes
* as a parameter
*/
echo $this->element('education_edit_form', array('hidden' => true, 'id' => $value['id']));
?>
<?php
}
?>
<div class="add-sub-menu">
<p><?php echo $add_another_position ?></p>
<?php echo $this->Html->image("plus_blue.png") ?>
</div>
<?php
/*
* Prints the form for adding... as no parameter is given as the id
* the form assumes it is a form for adding.
*/
echo $this->element('education_edit_form', array('hidden' => true));
?>
"education_edit_form"元素:
<?php
/* Prints a form for creating or editing an user academic entry.
*
* In case of edit form a variable id must be set with the identifier of
* the academic entry being creted.
*
* Other Paramaters:
* hiddden - Set to true if the form should be hidden by default
*
*/
if (isset($hidden)) {
($hidden) ? $hidden = 'display:none' : $hidden = '';
} else {
$hidden = '';
}
if (isset($id)){
$id_string = ".$id.";
}else{
$id_string = ".";
}
/* String Constants */
$input_eduschool_default_text = __("School");
$input_edudegree_default_text = __("Degree");
$input_edufield_default_text = __("Field/s of Education");
$input_edugrade_default_text = __("Grade (What qualification/s did you obtain?");
$input_edudescription_default_text = __("Description");
$input_edustartdate_default_text = __("Start Date");
$input_eduenddate_default_text = __("End Date");
$submit_button_text = __("Save");
?>
<?php echo $this->Form->create('Education', array('controller' => 'Education', 'action' => 'add', 'class' => 'main', 'style' => '$hidden')); ?>
<?php
echo $this->Form->input('Education'.$id_string.'school', array(
'class' => 'white large',
'placeholder' => $input_eduschool_default_text,
'id' => 'edu-school',
'label' => false,
'div' => false,
'type' => 'text'
));
?>
<?php
echo $this->Form->input('Education'.$id_string.'education', array(
'class' => 'white large',
'placeholder' => $input_edudegree_default_text,
'id' => 'edu-education',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'study_field', array(
'class' => 'white large',
'placeholder' => $input_edufield_default_text,
'id' => 'edu-field',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'qualification', array(
'class' => 'white large',
'placeholder' => $input_edugrade_default_text,
'id' => 'edu-grade',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'start_date', array(
'class' => 'white one-third',
'placeholder' => $input_edustartdate_default_text,
'id' => 'edu-start-date',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'end_date', array(
'class' => 'white one-third',
'placeholder' => $input_eduenddate_default_text,
'id' => 'edu-end-date',
'label' => false,
'div' => false,
'type' => 'text'
));
echo $this->Form->input('Education'.$id_string.'description', array(
'class' => 'white large',
'placeholder' => $input_edudescription_default_text,
'id' => 'edu-notes',
'label' => false,
'div' => false,
'type' => 'textarea',
'rows' => '5'
));
?>
<?php
echo $this->Form->submit($submit_button_text, array(
'class' => 'intro_submit',
'type' => 'submit'
));
echo $this->Form->end();
?>
请注意,这是一份草稿。。。我仍然需要执行操作来处理表单数据,并在'education_edit_form'元素中进行一些额外的验证。但现在我只想知道如何用正在编辑的条目中的值填充编辑表单。
提前感谢
我不确定我是否正确,但我认为您没有遵循Cake字段命名约定。
如果您想单独保存每个教育记录(即将过程作为一个单独的表格),您只需使用例如Education.school
作为字段名称。
如果你想同时保存所有的教育记录(即将它们作为一个大表单处理),你应该使用Education.0.school
、Education.1.school
名称,其中0
&1
只是单个记录的索引/计数器(即不是主键值)。
每个教育记录的id应添加到以下表格中:
echo $this->Form->hidden('Education.id'); // separate form per each record
或
echo $this->Form->hidden('Education.' . $i . '.id'); // one form for all records