2014-01-28 51 views
1

我純粹是cakephp的新手,目前正在構建1.3版本的項目。基本上我試圖顯示插入到數據庫中的提供者的城市名稱。我有兩個型號:gal_store.phpgal_location.php。在gal_store模型中,商店名稱與其對應的城市ID保存在gal_stores表中的city字段中。表gal_locations包含所有的城市和他們的名字。不在cakephp中工作的模型(JOIN)的關聯顯示空的結果

於是,我就加入這兩個表如下:

var $hasOne = array(
     'GalLocation' => array(
      'className' => 'GalLocation', 
      'foreignKey' => 'id', 
      'conditions' => '', 
      'fields' => '', 
      'order' => '' 
     ), 
    ); 

    function getList($limit = 50,$whether_list = false){ 
     $recursive = -1; 
    $conditions = array("GalStore.city"=> "GalLocation.id"); 
     //$conditions = ""; 
    $order = array("GalStore.address"); 
     if($whether_list == true){ 
      return $this->find("list",array("DISTINCT GalStore.city","recursive"=>$recursive,"limit"=>$limit,"order"=>$order,"conditions" => $conditions)); 
     }else{ 
      return $this->find("all",array("DISTINCT GalStore.city","recursive"=>$recursive,"limit"=>$limit,"order"=>$order,"conditions" => $conditions));  
     } 

    } 

但在ctp文件時,我做了var_dump($gal_locations);它總是顯示爲空!是什麼原因 ?

回答

0

如果gal_locations有gal_stores一一對應的關係,使用下面的代碼:

var $hasOne = array(
    'GalLocation' => array(
     'className' => 'GalLocation', 
     'foreignKey' => 'city',//if the city field contains the id of gal_locations table 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ), 
); 

如果gal_locations有一個與gal_stores一對多的關係,使用下面的代碼:

var $belongsTo = array(
    'GalLocation' => array(
     'className' => 'GalLocation', 
     'foreignKey' => 'city',//if the city field contains the id of gal_locations table 
     'conditions' => '', 
     'fields' => '', 
     'order' => '' 
    ), 
);