2014-06-15 38 views
0

我在兩個模型(代理和類別)之間有HABTM關係。我遵循這裏的說明: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html查找查詢不顯示CakePHP中引用表的結果使用HABTM

我已經設置了表格,創建了FKs等。爲了測試以查看是否正在爲代理找到查詢檢索類別,我將以下調試代碼:

debug($this->AgentsCategories->find('all', array(
    'order' => array('Agent.name', 'Agent.address'), 
    'conditions' => array('Agent.id' => '59') 
))); 

所得陣列:

array(
    (int) 0 => array(
     'Agent' => array(
      'id' => '59', 
      'name' => 'MUTUAL UNDERWRITERS', 
      'address' => 'Waipahu Branch 
94-615 Kupuohi St #102 
Waipahu, HI 96797', 
      'telephone' => '808-688-2222', 
      'fax' => '808-688-0769', 
      'email' => null, 
      'website' => 'http://www.mutualunderwriters.com', 
      'island' => '1', 
      'modified' => '2014-04-16 15:56:46' 
     ) 
    ) 
) 

僅示出信息從代理表,它不顯示從類別表,其中agents.id是FK類別。從指示,我會期望像下面的例子:

Array 
(
    [Recipe] => Array 
     (
      [id] => 2745 
      [name] => Chocolate Frosted Sugar Bombs 
      [created] => 2007-05-01 10:31:01 
      [user_id] => 2346 
     ) 
    [Ingredient] => Array 
     (
      [0] => Array 
       (
        [id] => 123 
        [name] => Chocolate 
       ) 
      [1] => Array 
       (
        [id] => 124 
        [name] => Sugar 
       ) 
      [2] => Array 
       (
        [id] => 125 
        [name] => Bombs 
       ) 
     ) 
) 

我在做什麼錯誤和/或我該如何調試呢?謝謝!

+1

顯示您的代理商型號代碼 –

回答

0

您代理模式應該是這樣的:

class Agent extends AppModel { 
public $hasAndBelongsToMany = array(
    'Category' => 
     array(
      'className' => 'Category', 
      'joinTable' => 'agents_categories', //or your table name 
      'foreignKey' => 'agent_id', 
      'associationForeignKey' => 'category_id', 
      'unique' => true, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'finderQuery' => '', 
      'with' => '' 
     ) 
); 
} 

,簡單地做這樣一個搜索:

$this->Agent->find('all', array('order' => array('Agent.name', 'Agent.address'), 
           'conditions' => array('Agent.id' => '59'))); 

你會得到預期的結果。

+1

兩個單詞應該在回答'遞歸'和'可容納'=)。 – AD7six

+0

感謝您的幫助,我的代理模型看起來像這樣,查詢看起來也是這樣,但輸出不匹配沒有顯示類別數組的示例。 – dev808

+0

Hi @ AD7six,你的意見是什麼意思? – dev808