Cakephp如何在索引视图中添加新记录
我是cakePHP新手,想知道如何在索引视图中添加一个表单来向数据库添加新记录。我的意思是,在Index.ctp中,可以显示记录列表,下面是一些占位符,带有添加按钮以插入dtb。我必须修改控制器吗 我试图在索引视图中添加一个表单,目标是../add,但在单击submit之后,它总是重定向到../add/{number},我必须重新提交信息。以下是我试图修改的代码:Cakephp如何在索引视图中添加新记录,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我是cakePHP新手,想知道如何在索引视图中添加一个表单来向数据库添加新记录。我的意思是,在Index.ctp中,可以显示记录列表,下面是一些占位符,带有添加按钮以插入dtb。我必须修改控制器吗 我试图在索引视图中添加一个表单,目标是../add,但在单击submit之后,它总是重定向到../add/{number},我必须重新提交信息。以下是我试图修改的代码: <div class="departments index large-9 medium-8 columns content"
<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>
看起来不错,您需要在索引方法中设置
$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() ?>
浏览博客教程@ManoharKhadka博客教程演示如何在不同视图(add.ctp、edit.ctp、view.ctp、index.ctp)中创建博客的视图编辑和视图列表。但是我想在同一个视图上同时执行函数Add()和Index()。CTP你能帮我回答下面的问题吗,我不确定它是否正确,但它确实有效。当然它是正确的。无论你在Add方法中做什么,你也可以在Index方法中做。一点问题都没有。。
<?= $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() ?>