user3576366
user3576366

Reputation: 15

how to write joining two table query in cakephp

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

How to write this query in cakephp controller and how to display result in view part .

Upvotes: 1

Views: 2935

Answers (4)

Cognus Test
Cognus Test

Reputation: 43

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

if you join multiple database connection database are on same server otherwise not working

Upvotes: 3

rakeshpatil919
rakeshpatil919

Reputation: 1

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

Upvotes: 0

Sharma Vikram
Sharma Vikram

Reputation: 2480

you should try this Employee is your Model name, stores is your table name and type is your joining left,right and inner

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

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

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

Upvotes: 0

Related Questions