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);

我想用分页设置它。

我怎样才能做到这一点?

4

3 回答 3

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);

希望对你有帮助

谢谢

于 2012-09-24T05:01:11.990 回答
0

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

$this->paginate = array('TravancoDSREmployee' => $options);
$result = $this->paginate('TravancoDSREmployee');
$this->set('result', $result);
于 2012-09-24T05:10:25.247 回答
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

于 2012-09-24T09:18:39.007 回答