如何在cakephp中将连接查询结果更改为$ this-> paginate

时间:2012-09-24 04:55:06

标签: join pagination cakephp-2.0

这是我的加入查询代码:

$options['joins'] = array(
                array('table' => 'employees',
                    'alias' => 'Employees',
                    'type' => 'INNER',
                    'foreignKey' => 'employee_id',
                    // 'fields' => 'Users.user', <- get rid of this
                    'conditions' => array(
                        'TravancoDSREmployee.emp_id = Employees.employee_id'
                    )
                )
            );
        $options['fields'] = array('TravancoDSREmployee.*','Employees.*'); // <- insert this
        $options['conditions'] = array( 'TravancoDSREmployee.created_emp_id' => $emp_id);
        $result = $this->TravancoDSREmployee->find('all', $options);
        $this->set('result', $result);

我想用分页设置它。

我该怎么做?

3 个答案:

答案 0 :(得分:1)

请查看下面的网址。

http://garbers.co.za/2011/05/03/easy-way-to-generate-cakephp-habtm-joins-for-use-in-pagination/

代码您可以尝试

$fields = array('TravancoDSREmployee.*','Employees.*'); // <- insert this
$conditions = array( 'TravancoDSREmployee.created_emp_id' => $emp_id);
$joins = array();
$joins[] = array(
    'table' => 'employees',
            'alias' => 'Employees',
            'type' => 'INNER',
            'foreignKey' => 'employee_id',
            // 'fields' => 'Users.user', <- get rid of this
            'conditions' => 'TravancoDSREmployee.emp_id = Employees.employee_id',
      );

$this->paginate = compact('fields' , 'joins' , 'conditions');
$result = $this->paginate('TravancoDSREmployee');
$this->set('result', $result);

我希望它会对你有所帮助,

感谢

答案 1 :(得分:0)

首先,您必须将条件传递给控制器​​中的paginate属性。之后,您可以调用paginate函数。将您的$结果变为:

$this->paginate = array('TravancoDSREmployee' => $options);
$result = $this->paginate('TravancoDSREmployee');
$this->set('result', $result);

答案 2 :(得分:0)

好的,我明白了:

这对我有用:

$this->paginate['TravancoDSREmployee'] = array(
            'limit' => 3, 
            'contain' => '', 
            'conditions' => array('TravancoDSREmployee.created_emp_id' => $emp_id),
            'fields' => array('TravancoDSREmployee.*','Employees.*'),
            'joins' => array(array('table' => 'employees', 'type' => 'INNER', 'alias' => 'Employees', 'conditions' => array('TravancoDSREmployee.emp_id = Employees.employee_id')))
            );
        $result = $this->paginate('TravancoDSREmployee');
        $this->set('result',$result);

我提到这个链接以获得解决方案: http://planetcakephp.org/aggregator/items/3544-using-mysql-inner-join-in-cakephp-pagination