2017-05-19 49 views
0

我對cakePHP的新增功能以及想知道如何在索引視圖中添加表單以向數據庫添加新記錄。我的意思是在Index.ctp中,可以顯示記錄列表,並在其下方顯示一些帶有添加按鈕的佔位符以插入dtb。我必須修改控制器嗎?Cakephp如何在索引視圖中添加新記錄

我試圖在索引視圖中添加一個表單,目的地是../add,但是在點擊提交後,它總是重定向到../add/{number},我必須重新提交信息。這裏是代碼即時試圖修改:

<div class="departments index large-9 medium-8 columns content"> 
<h3><?= __('Departments') ?></h3> 
<table cellpadding="0" cellspacing="0"> 
    <thead> 
     <tr> 
      <th scope="col"><?= $this->Paginator->sort('id') ?></th> 
      <th scope="col"><?= $this->Paginator->sort('name') ?></th> 
      <th scope="col"><?= $this->Paginator->sort('modified') ?></th> 
      <th scope="col" class="actions"><?= __('Actions') ?></th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php foreach ($departments as $department): ?> 
     <tr> 
      <td><?= $this->Number->format($department->id) ?></td> 
      <td><?= h($department->name) ?></td> 
      <td><?= h($department->modified) ?></td> 
      <td class="actions"> 
       <?= $this->Html->link(__('View'), ['action' => 'view', $department->id]) ?> 
       <?= $this->Html->link(__('Edit'), ['action' => 'edit', $department->id]) ?> 
       <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $department->id], ['confirm' => __('Are you sure you want to delete # {0}?', $department->id)]) ?> 
      </td> 
     </tr> 
     <?php endforeach; ?> 
    </tbody> 
</table> 
<div class="paginator"> 
    <ul class="pagination"> 
     <?= $this->Paginator->first('<< ' . __('first')) ?> 
     <?= $this->Paginator->prev('< ' . __('previous')) ?> 
     <?= $this->Paginator->numbers() ?> 
     <?= $this->Paginator->next(__('next') . ' >') ?> 
     <?= $this->Paginator->last(__('last') . ' >>') ?> 
    </ul> 
    <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p> 
</div> 
<div class="departments form large-9 medium-8 columns content"> 
<!-- $this->Form->create("Post",array('action'=>'add')); --> 
    <?= $this->Form->create($department,['url' => ['action' => 'add']]) ?> 
    <fieldset> 
     <legend><?= __('Add Department') ?></legend> 
     <?php 
      echo $this->Form->control('name'); 
      echo $this->Form->control('description'); 
      echo $this->Form->control('subjects._ids', ['options' => $subjects]); 
     ?> 
    </fieldset> 
    <?= $this->Form->button(__('Submit')) ?> 
    <?= $this->Form->end() ?> 
</div> 

在控制器:

public function index() 
{ 
    $departments = $this->paginate($this->Departments); 

    // $this->add(); 
    $subjects = $this->Departments->Subjects->find('list', ['limit' => 200]); 
    $this->set(compact('departments', 'subjects')); 
    $this->set('_serialize', ['departments']); 


} 

public function add() 
{ 
    $department = $this->Departments->newEntity(); 
    if ($this->request->is('post')) { 
     $department = $this->Departments->patchEntity($department, $this->request->getData()); 
     if ($this->Departments->save($department)) { 
      $this->Flash->success(__('The department has been saved.')); 

      return $this->redirect(['action' => 'index']); 
     } 
     $this->Flash->error(__('The department could not be saved. Please, try again.')); 
    } 
    $subjects = $this->Departments->Subjects->find('list', ['limit' => 200]); 
    $this->set(compact('department', 'subjects')); 
    $this->set('_serialize', ['department']); 
} 
+0

-examples/blog/blog.html –

+0

@ManoharKhadka博客教程展示瞭如何在不同視圖(add.ctp,edit.ctp,view.ctp,index.ctp)中創建視圖編輯和查看博客列表。但我想同時執行這兩個函數Add()和Index()在同一視圖.ctp – vicnoob

回答

0

它看起來不錯,你需要設置$department索引方法以及你的形式是索引頁和表格正在使用$department變量。使您的索引方法如下所示:

public function index() 
{ 
    $departments = $this->paginate($this->Departments); 
    $department = $this->Departments->newEntity(); // added 
    // $this->add(); 
    $subjects = $this->Departments->Subjects->find('list', ['limit' => 200]); 
    $this->set(compact('departments', 'subjects,'department')); // edited 
    $this->set('_serialize', ['departments']); 


} 
+0

你能幫我下面的答案,我不知道它是否正確,但它的工作 – vicnoob

+0

當然是正確的..無論你在添加方法中做什麼,你都可以在索引方法中做到這一點.. ..沒有問題.. –

0

我自己找到了答案,它有效,但我不知道這種情況是否正確。

我把所有的控制器和上面一樣,只是改變index.ctp形式經過博客教程.. https://book.cakephp.org/3.0/en/tutorials-and

<?= $this->Form->create(null,['url' => ['action' => 'add']]) ?> 
    <fieldset> 
     <legend><?= __('Add Department') ?></legend> 
     <?php 
      echo $this->Form->control('name'); 
      echo $this->Form->control('description'); 
      echo $this->Form->control('subjects._ids', ['options' => $subjects]); 
     ?> 
    </fieldset> 
    <?= $this->Form->button(__('Submit')) ?> 
    <?= $this->Form->end() ?>