How do I write a join query across multiple tables in CakePHP?

后端 未结 6 1686
既然无缘
既然无缘 2020-11-28 07:50

can anyone tell me, how to retrieve joined result from multiple tables in cakePHP ( using cakePHP mvc architecture). For example, I have three tables to join (tbl_topics, tb

6条回答
  •  日久生厌
    2020-11-28 08:28

    I think this answer is already submitted, but I am posting here for someone who seeks still for this. The joins can be done with find() method can be like below

    $result = $this->ModelName1->find("all",array(
    'fields' => array('ModelName1.field_name','Table2.field_names'), // retrieving fileds 
    'joins' => array(  // join array
        array(
            'table' => 'table_name',
            'alias' => 'Table2',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions'=> array('ModelName1.id = Table2.id') // joins conditions array
        ),
        array(
            'table' => 'table_name3',
            'alias' => 'Table3',
            'type' => 'inner',
            'foreignKey' => false,
            'conditions'=> array('Table3.id = Table2.id')
        )
    ))); 
    

提交回复
热议问题