Ich bin neu bei cakePHP und frage mich, wie ich ein Formular in der Indexansicht hinzufügen kann, um der Datenbank einen neuen Datensatz hinzuzufügen. Ich meine, in der Index.ctp kann die Liste der Datensätze angezeigt werden und darunter befinden sich einige Platzhalter mit einer Hinzufügen-Schaltfläche zum Einfügen in die DTB. Muss ich den Controller ändern?CakePHP wie man einen neuen Datensatz in der Indexansicht hinzufügt
Ich habe versucht, ein Formular in der Indexansicht hinzuzufügen, mit dem Ziel ist ../add, aber nach dem Klick reichen Sie immer umleiten auf die ../add/{number} und ich muss die Informationen erneut einreichen. Hier wird der Code im zu ändern versuchen:
<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>
auf dem 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']);
}
Go durch Blog-Tutorial .. https://book.cakephp.org/3.0/en/tutorials-and -examples/blog/blog.html –
@ManoharKhadka Blog-Tutorial zeigt, wie View-Edit und View-Liste von Blogs in verschiedenen Ansichten erstellen (add.ctp, edit.ctp, view.ctp, index.ctp). Aber ich möchte beide Funktionen ausführen Add() und Index() auf der gleichen view.ctp – vicnoob