0

我有两张桌子:

  1. evaluation_employees 字段:id、用户名
  2. 评估结果字段:id、等级、评估员工 ID

在评估结果视图中,我需要通过员工的用户名搜索结果(使用cakephp 搜索插件),但在评估结果表中,我有员工的 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
            );
    }

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

4

1 回答 1

0

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

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

于 2012-06-03T23:54:47.217 回答