Join query using two table with where condition in cake php?

扶醉桌前 提交于 2019-12-12 02:57:59

问题


How we can write Join query using two table with where condition in cake php?


回答1:


You can!!
check this if it would help you

$table = 'table_name';
    $query['conditions'] = array($table.'.entity_id' => $entity_id, $table.'.is_active' => 1);

        $query['fields'] = array('creator.first_name AS cf_name', 'creator.last_name AS cl_name', 'creator.email AS c_email', 'usr.first_name', 'usr.last_name',
            $table.'.id AS id', $table.'.guid', $table.'.updated_date',
            'usr.email AS email');

        // To do joining to get attribute with value
        $query['joins'] = array(
            array(
                    'table' => $this->user,
                    'alias' => 'usr',
                    'type' => 'INNER',
                    'conditions' => array('usr.id = '.$table.'.user_id')
                ),
                array(
                    'table' => $this->user,
                    'alias' => 'creator',
                    'type' => 'INNER',
                    'conditions' => array('creator.id = '.$table.'.creator_id')
                ),
        );
        $query['order'] = array($table.'.updated_date' => 'DESC');

        // Cache implementation
        $result = $this->find('all', $query);


来源:https://stackoverflow.com/questions/16165345/join-query-using-two-table-with-where-condition-in-cake-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!