簡體   English   中英

Cakephp如何在索引視圖中添加新記錄

[英]Cakephp how to add new record in the index view

我是cakePHP的新手,想知道如何在索引視圖中添加表單以將新記錄添加到數據庫。 我的意思是在Index.ctp中,可以顯示記錄列表,並且在記錄列表的下面是一些占位符,帶有添加按鈕可插入dtb。 我需要修改控制器嗎?

我試圖在索引視圖中添加一個表單,目標是../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']);
}

看起來不錯,您需要在索引方法中設置$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']);


}

好吧,我自己找到了答案,它可以工作,但是我不知道這種情況是否正確。

我將所有控制器保持與上述相同,只是更改index.ctp中的形式

<?= $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() ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM