简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM