【问题标题】:Cakephp how to add new record in the index viewCakephp如何在索引视图中添加新记录
【发布时间】:2017-05-19 02:49:35
【问题描述】:

我是 cakePHP 新手,想知道如何在索引视图中添加表单以向数据库添加新记录。我的意思是在 Index.ctp 中,可以显示记录列表,并且在其下方是一些占位符,其中包含要插入到 dtb 的添加按钮。需要修改Controller吗?

我尝试在索引视图中添加一个表单,目的地是 ../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']);
}

【问题讨论】:

  • @ManoharKhadka 博客教程展示了如何在不同的视图(add.ctp、edit.ctp、view.ctp、index.ctp)中创建视图编辑和查看博客列表。但我想在同一个 view.ctp 上同时执行函数 Add() 和 Index()

标签: php cakephp cakephp-3.0


【解决方案1】:

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


}

【讨论】:

  • 你能帮我回答下面的问题吗,我不确定它是否正确,但它有效
  • 当然是对的..无论你在add方法中做什么你也可以在index方法中做到这一点..完全没问题..
【解决方案2】:

好吧,我自己找到了答案,它有效,但我不知道这种情况是否正确。

我把所有的控制器都和上面一样,只是在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() ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多