简体   繁体   English

Cakephp如何在索引视图中添加新记录

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

Im new to cakePHP and wondering how can i add a form in the index view to add new record to the database. 我是cakePHP的新手,想知道如何在索引视图中添加表单以将新记录添加到数据库。 I mean in the Index.ctp, the list of record can be display and below it is some placeholders with an add button to insert to the dtb. 我的意思是在Index.ctp中,可以显示记录列表,并且在记录列表的下面是一些占位符,带有添加按钮可插入dtb。 Do i have to modify the Controller? 我需要修改控制器吗?

I tried to add a form in index view, with the destination is ../add, but after click submit it always redirect to the ../add/{number} and i have to re-submit the info. 我试图在索引视图中添加一个表单,目标是../add,但是单击提交后,它总是重定向到../add/{number},我必须重新提交信息。 Here is the code im trying to modify: 这是我正在尝试修改的代码:

<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>

On the controller: 在控制器上:

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']);
}

It looks good,you need to set $department in index method as well as your form is in index page and the form is using $department variable.Make your index method something like this: 看起来不错,您需要在索引方法中设置$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']);


}

Well i found the answer myself, it works, but i dont know if it is the right way this situation. 好吧,我自己找到了答案,它可以工作,但是我不知道这种情况是否正确。

I keep all the controllers same as above, just change the form in index.ctp 我将所有控制器保持与上述相同,只是更改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