2016-12-15 20 views
0

我想使用Cakephp为医院管理系统创建一个简单的网站。我想在我的“患者”控制器中使用搜索方法,该方法将生成一个表单并将PatientID作为用户的输入。然后它会生成该特定行的所有值的结果。我如何去做,我需要在模型,控制器和模板中做什么改变?使用表单搜索特定行以获取数据并在Cakephp中获取详细结果

的PatientsTable如下:预先

<?php 
namespace App\Model\Table; 

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

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('patients'); 
    $this->displayField('Patient_ID'); 
    $this->primaryKey('Patient_ID'); 
} 

/** 
* Default validation rules. 
* 
* @param \Cake\Validation\Validator $validator Validator instance. 
* @return \Cake\Validation\Validator 
*/ 
public function validationDefault(Validator $validator) 
{ 
    $validator 
     ->allowEmpty('Patient_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 
     ->allowEmpty('Blood_Group'); 

    return $validator; 
} 
} 

感谢。

回答

0

你应该看一下在CakePHP中搜索的一个很棒的插件,例如。 https://github.com/friendsofcake/search

然后,所有你需要的是:

初始化方法表类:

public function initialize(array $config) 
{ 
    parent::initialize($config); 

    $this->table('patients'); 
    $this->displayField('Patient_ID'); 
    $this->primaryKey('Patient_ID'); 

    // Add the behaviour to your table 
    $this->addBehavior('Search.Search'); 

    // Setup search filter using search manager 
    $this->searchManager() 
     ->value('Patient_ID'); 
} 

例控制器:

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

public function index() 
{ 
    $query = $this-> Patients 
     ->find('search', ['search' => $this->request->query]); 
    $this->set('patients', $this->paginate($query)); 
} 

然后,你可以简单地使用像一个URL查询参数/患者/指数?Patient_ID = 14以仅显示Patient_ID中具有该值的患者。

这可以用CakePHP的形式助手来完成:

echo $this->Form->create(); 
echo $this->Form->input('Patient_ID'); 
echo $this->Form->button('Search', ['type' => 'submit']); 
echo $this->Form->end(); 

Discalimer:我偷了所有的示例代码:https://github.com/friendsofcake/search

你应该检查出不同类型的过滤器表中的使用,那些知道的真棒:https://github.com/friendsofcake/search#filters - 和往常一样,阅读自述文件! :-)

祝你好运搜索和过滤!

相关问题