如何在cakephp中编写连接两个表查询

时间:2015-02-02 06:55:12

标签: cakephp cakephp-2.0 cakephp-1.3 cakephp-2.1 cakephp-2.3

SELECT e.employee_name, e.employee_store, e.employee_phone, s.store_address
FROM  `employee` e
JOIN store s ON e.employee_store = s.store_name

如何在cakephp控制器中编写此查询以及如何在视图部分中显示结果。

4 个答案:

答案 0 :(得分:3)

$this->Model->find(
   'all',
  array(
   'fields' => array('table.employee_name', 'table.employee_store', ....),
   'joins' => array(
          'table' => 'databasename.store',
          'conditions' => array('employee_store' => 'store_name')
       )
    )
)

如果您加入多个数据库连接 数据库位于同一服务器上,否则将无法正常工作

答案 1 :(得分:0)

尝试 -

$this->Model->find(
'all',
array(
 'fields' => array('table.employee_name', 'table.employee_store', ....),
 'joins' => array(
                'table' => 'store',
                'conditions' => array('employee_store' => 'store_name')
            )
)
)

答案 2 :(得分:0)

  你应该试试这个     员工是您的模型名称,商店是您的表名称,类型是您的加入左,右和内

$details=$this->Employee->find('all',array('fields' => array('Employee.*','stores.*'),
        'joins'=>array(
                array(
                    'table'=>'store',
                    'type'=>'inner',
                    'conditions'=>array('Emmployee.employee_store=stores.store_name')
                    )
                )
            )
      );

答案 3 :(得分:0)

$this->employee->bindModel(
    array('hasMany' => array(
        'Store' => array(
            'className' => 'Principle'
                )
            )
        )
    );