在Cakephp中的同一页面上返回查询结果

时间:2016-12-26 05:06:23

标签: cakephp cakephp-3.0

我正在医院管理系统上创建一个网站。有一个Employee控制器,它有一个类型列,指定员工是医生,医务人员还是管理员。在这个页面上我有一个动作"医生"点击后应返回" Doctor"只在同一页面上。我需要在控制器和模板中进行哪些更改?

员工表:

<?php
namespace App\Model\Table;

use Search\Manager;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class EmployeeTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('employee');
        $this->displayField('Employee_ID');
        $this->primaryKey('Employee_ID');
        $this->addBehavior('Search.Search');
        $this->searchManager()
             ->value('Employee_ID');
    }

    public function validationDefault(Validator $validator)
    {
        $validator
            ->allowEmpty('Employee_ID', 'create');

        $validator
            ->requirePresence('Name', 'create')
            ->notEmpty('Name');

        $validator
            ->requirePresence('Address', 'create')
            ->notEmpty('Address');

        $validator
            ->date('DOB')
            ->requirePresence('DOB', 'create')
            ->notEmpty('DOB');

        $validator
            ->allowEmpty('Contact');

        $validator
            ->requirePresence('Gender', 'create')
            ->notEmpty('Gender');

        $validator
            ->numeric('Salary')
            ->allowEmpty('Salary');

        $validator
            ->allowEmpty('Category');

        $validator
            ->requirePresence('Dept_No', 'create')
            ->notEmpty('Dept_No');

        return $validator;
    }
}

员工控制员:

<?php
namespace App\Controller;

use App\Controller\AppController;

class EmployeeController extends AppController
{

    public function initialize()
{
    parent::initialize();
    $this->loadComponent('Search.Prg', [
        'actions' => ['index']
    ]);
}

    public function index()
    {
        $employee = $this->paginate($this->Employee);

        $this->set(compact('employee'));
        $this->set('_serialize', ['employee']);
        $query = $this->Employee
        ->find('search', ['search' => $this->request->query]);
    $this->set('patients', $this->paginate($query));
    }

    public function view($id = null)
    {
        $employee = $this->Employee->get($id, [
            'contain' => []
        ]);

        $this->set('employee', $employee);
        $this->set('_serialize', ['employee']);
    }

    public function add()
    {
        $employee = $this->Employee->newEntity();
        if ($this->request->is('post')) {
            $employee = $this->Employee->patchEntity($employee, $this->request->data);
            if ($this->Employee->save($employee)) {
                $this->Flash->success(__('The employee has been saved.'));

                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The employee could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('employee'));
        $this->set('_serialize', ['employee']);
    }

    public function edit($id = null)
    {
        $employee = $this->Employee->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $employee = $this->Employee->patchEntity($employee, $this->request->data);
            if ($this->Employee->save($employee)) {
                $this->Flash->success(__('The employee has been saved.'));

                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The employee could not be saved. Please, try again.'));
            }
        }
        $this->set(compact('employee'));
        $this->set('_serialize', ['employee']);
    }

    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $employee = $this->Employee->get($id);
        if ($this->Employee->delete($employee)) {
            $this->Flash->success(__('The employee has been deleted.'));
        } else {
            $this->Flash->error(__('The employee could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}

员工模板:

<nav class="large-3 medium-4 columns" id="actions-sidebar">
    <ul class="side-nav">
        <li class="heading"><?= __('Actions') ?></li>
        <li><?= $this->Html->link(__('New Employee'), ['action' => 'add']) ?></li>
        <li><?= $this->Html->link(__('Doctors') ?></li>
        <li><?= $this->Html->link(__('Medical staff') ?></li>
        <li><?= $this->Html->link(__('Administration') ?></li>


    </ul>
</nav>

<div class="employee form large-9 medium-8 columns content">
    <?= $this->Form->create() ?>
    <fieldset>
        <legend><?= __('Search Employee') ?></legend>
        <?= $this->Form->input('Employee_ID',array('type' => 'text', 'label' => 'ID')); ?> 
    </fieldset>
    <?= $this->Form->button('Search', ['type' => 'submit']); ?>
    <?= $this->Form->end() ?>
</div>


<div class="employee index large-9 medium-8 columns content">
    <h3><?= __('Employee') ?></h3>
    <table cellpadding="0" cellspacing="0">
        <thead>
            <tr>
                <th scope="col"><?= $this->Paginator->sort('Employee_ID') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Name') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Address') ?></th>
                <th scope="col"><?= $this->Paginator->sort('DOB') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Contact') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Gender') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Salary') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Category') ?></th>
                <th scope="col"><?= $this->Paginator->sort('Dept_No') ?></th>
                <th scope="col" class="actions"><?= __('Actions') ?></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($employee as $employee): ?>
            <tr>
                <td><?= h($employee->Employee_ID) ?></td>
                <td><?= h($employee->Name) ?></td>
                <td><?= h($employee->Address) ?></td>
                <td><?= h($employee->DOB) ?></td>
                <td><?= h($employee->Contact) ?></td>
                <td><?= h($employee->Gender) ?></td>
                <td><?= $this->Number->format($employee->Salary) ?></td>
                <td><?= h($employee->Category) ?></td>
                <td><?= h($employee->Dept_No) ?></td>
                <td class="actions">
                    <?= $this->Html->link(__('View'), ['action' => 'view', $employee->Employee_ID]) ?>
                    <?= $this->Html->link(__('Edit'), ['action' => 'edit', $employee->Employee_ID]) ?>
                    <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $employee->Employee_ID], ['confirm' => __('Are you sure you want to delete # {0}?', $employee->Employee_ID)]) ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <div class="paginator">
        <ul class="pagination">
            <?= $this->Paginator->prev('< ' . __('previous')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('next') . ' >') ?>
        </ul>
        <p><?= $this->Paginator->counter() ?></p>
    </div>
</div>

提前致谢!

1 个答案:

答案 0 :(得分:0)

在索引函数if ($this->request->is('get')) { //do something }

中使用它