2013-11-25 96 views
1

我有一個場景,其中有兩個模型,配置文件和教育,配置文件hasMany教育關係。CakePHP 2.4中的同一模型的多個編輯表單

然後,我有,我想有多種形式一種觀點:

爲用戶配置文件的每個教育進入
  • 一個編輯的形式。
  • 添加新條目的一種形式。

這裏的問題是,我想編輯表格填寫每個條目的數據。 關於如何實現這一點的任何想法?

CakePHP的文件指出:

的表單助手使用$這個 - >請求 - >數據屬性來自動檢測是否創建添加或編輯表單。如果$這個 - >請求 - >數據包含表單模型命名的數組元素,該數組包含模型的主鍵的非空值,那麼表單助手將創建一個編輯表單該記錄

我已經成功實現了一個編輯表單,但是在只有一個表單的情況下。

現在,我有這樣的事情:

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'元素中進行一些額外的驗證。但現在我只想知道如何用正在編輯的條目中的值填充編輯表單。

預先感謝您

回答

0

我不知道,如果我得到這個權利,但我覺得你沒有關注蛋糕field naming conventions

如果您想單獨保存每份教育記錄(即作爲單獨的表格處理),您應該簡單地使用Education.school作爲字段名稱。

如果你想保存所有教育記錄,在相同(即處理它們作爲一個大的形式),你應該使用Education.0.schoolEducation.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 
相關問題