Cakephp - 按不同表的字段搜索

时间:2012-04-26 00:55:12

标签: php cakephp search cakephp-1.3

我有两张桌子:

  1. evaluation_employees 字段:id,用户名
  2. evaluation_results 字段:id,grade,evaluation_employee_id
  3. 在evaluation_results视图中,我需要通过员工的用户名搜索结果(使用cakephp search plugin),但在evaluate_results表中,我有员工的id(evaluation_employee_id),而不是用户名。我不知道如何在这些表之间建立链接,使其工作。

    我视图中的代码(evaluation_results / index.ctp):

    echo $this->Form->create('EvaluationResult', array(
                'url' => array_merge(array('action' => 'index'), $this->params['pass'])
                ));
    echo $this->Form->input('username', array('div' => false, 'empty' => true)) . '<br/><br/>';
    echo $this->Form->submit(__('Search', true), array('div' => false));
    echo $this->Form->end();
    

    我的控制器中的代码(evaluation_results_controller.php):

    var $components = array('Search.Prg');
        function index() {
            $this->Prg->commonProcess();
            $this->EvaluationResult->recursive = 0;
            $this->paginate = array(
                        'conditions' => $this->EvaluationResult->parseCriteria($this->passedArgs));
            $this->set('evaluationResults', $this->paginate());
        }
    

    我的模型中的代码(evaluation_result.php):

    public $actsAs = array('Search.Searchable');
    
        //Search fields data description for processing.
        public $filterArgs = array(
            array('name' => 'EvaluationEmployee.username', 'type' => 'query', 'method' => 'filterUsername')
        );
    
        //Method as decalred in $filterArgs to process the free form search.
        public function filterUsername($data, $field = null) {
            if (empty($data['EvaluationEmployee']['username'])) {
                return array();
            }
            $username = '%' . $data['EvaluationEmployee']['username'] . '%';
            return array(
                    $this->alias . '.EvaluationEmployee.username LIKE' => $username
                );
        }
    

    请告诉我是否需要更多信息。

1 个答案:

答案 0 :(得分:0)

如果您在模型中设置关联,它会在您搜索员工时自动找到所有相关记录。

http://book.cakephp.org/1.3/en/view/1039/Associations-Linking-Models-Together