2015-04-04 85 views
0

我想將這個mysql查詢轉換成cakephp,但它不工作。CakePHP Inner Join Not Working

這裏是我的MySQL查詢

SELECT r.id, p.name, r.paid, p.created 
     FROM patients p 
     INNER JOIN reports r ON p.id = r.patient_id 
     AND doctor_name LIKE '%dr.saidul%' 

在CakePHP我曾嘗試婁代碼

$query_options = array(); 
      $query_options['fields'] = array('Report.id', 'Report.paid','Patient.name','Patient.created'); 
      $query_options['conditions'] = array('Patient.doctor_name'=>'%dr.saidul%'); 
      $query_options['joins'] = array('table' => 'Report', 
            'type' => 'INNER', 
            'conditions' => array(
             'Patient.id = Report.id', 
            ) 
           ); 

      $patientlist=$this->Patient->find('all', $query_options); 

檢查sql_dump之後它給我怒吼結果

SQL Query: SELECT `Report`.`id`, `Report`.`paid`, `Patient`.`name`, `Patient`.`created` FROM `diagnosis`.`patients` AS `Patient` LEFT JOIN `diagnosis`.`upazilas` AS `Upazila` ON (`Patient`.`upazila_id` = `Upazila`.`id`) LEFT JOIN `diagnosis`.`zilas` AS `Zila` ON (`Patient`.`zila_id` = `Zila`.`id`) LEFT JOIN `diagnosis`.`doctors` AS `Doctor` ON (`Patient`.`doctor_id` = `Doctor`.`id`) INNER JOIN `diagnosis`.`Report` ON (`Patient`.`id` => `Report`.`id`) WHERE `Patient`.`doctor_name` = '%dr.saidul%' 

以下錯誤:SQLSTATE [ 42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在'=>Report附近使用正確的語法。 id)其中Patientdoctor_name ='%dr.saidul%''at line 1

+0

你得到了什麼錯誤? – 2015-04-04 05:38:39

+0

陣列(陣列( '表'=> '報告', '類型'=> '內', '條件'=>數組( 'Patient.id = Report.id', )) ); – 2015-04-04 05:39:28

+0

您是否加載了報告模型? – 2015-04-04 05:46:27

回答

0
I have submitted the cakephp inner join code please check it 

$query_options = array(); 
      $query_options['fields'] = array('reports.id', 'reports.paid','Patient.name','Patient.created','Patient.doctor_name'); 
      $query_options['conditions'] = array('Patient.doctor_name'=>'%dr.saidul%'); 
      $query_options['joins'] = array(
           array('table' => 'reports', 
           'type' => 'INNER', 
           'conditions' => array(
            'Patient.id = reports.id', 
            ) 
           ) 
          ); 
      $patientlist=$this->Patient->find('all', $query_options);