2012-11-07 45 views
2

我無法使用hasMany通過(連接模型)關聯獲取關聯的模型數據。 我正在使用cakephp 2.2.3 在cakephp 1.3中也有同樣的發現,所以我不知道怎麼回事...cakephp 2.2.3 - 無法通過hasMany獲取關聯數據(連接模型)

事件hasMany ScoresToEvent。分數有許多分數要事件。 ScoresToEvent belongsTo事件和分數。

ScoresToEvent將有額外的信息,所以我不能使用HATBM。

一些代碼:

event.php  
class Event extends AppModel{ 
     public $name='Event'; 
     public $hasMany=array('ScoresToEvent'); 
     public $belongsTo=array('Entity'); 
     public $actsAs=array('containable'); 
} 

score.php 
class Score extends AppModel{ 
     public $name='Score'; 
     public $hasMany=array('ScoresToEvent'); 
     public $belongsTo=array('Entity'); 
} 

scores_to_event.php 
class ScoresToEvent extends AppModel{ 
    public $name='ScoresToEvent'; 
    public $belongsTo=array('Event','Score'); 
} 

當我檢索數據,我得到這些結果:

$this->Event->ScoresToEvent->find('all', array('recursive'=>2)) 
array(
    (int) 0 => array(
     'ScoresToEvent' => array(
      'id' => '8', 
      'event_id' => '7', 
      'score_id' => '1' 
     ) 
    ), 
    (int) 1 => array(
     'ScoresToEvent' => array(
      'id' => '9', 
      'event_id' => '7', 
      'score_id' => '3' 
     ) 
    ) 
) 

在這種情況下,我必須讓事件和得分數據。

如果我嘗試使用中可容納它返回模型「ScoresToEvent」不與模型相關的「分數」這陣,因此它並沒有檢索得分數據...

$this->Event->find('all', array(
     'contain'=>array(
     'Entity', 
     'ScoresToEvent'=>array('Score'=>array('Entity')) 
    ), 
     'conditions'=>array('Event.id'=>7)); 

array(
    (int) 0 => array(
     'Event' => array(
      'id' => '7', 
      'entity_id' => '17', 
      'start_date' => '2012-07-24', 
      'status' => null, 
      'end_date' => null 
     ), 
     'Entity' => array(
      'id' => '17', 
      'title' => 'y', 
      'content' => '', 
      'subtitle' => '', 
      'type' => 'Evento', 
      'avatar' => null, 
      'image' => null 
     ), 
     'ScoresToEvent' => array(
      (int) 0 => array(
       'id' => '8', 
       'event_id' => '7', 
       'score_id' => '1' 
      ), 
      (int) 1 => array(
       'id' => '9', 
       'event_id' => '7', 
       'score_id' => '3' 
      ) 
     ) 
    ) 
) 

我的錯在哪裏?哪部分代碼錯了? 我想這對一個全新的CakePHP 2.2.3安裝

感謝所有

附: cakephp 1.3中的相同代碼正確工作 p.p.s.不要考慮'實體'。

回答

3

我認爲是模型名稱的問題。嘗試將文件scores_to_event.php重命名爲ScoresToEvent.php。 但在這種情況下,我覺得bestname OFR這種模式是:ScoreEvent.php,是比較合適的。 嘗試將更多的信息,你的模型像這樣的例子後: 到ScoreEvent.php

public $belongsTo = array(
     'Event' => array(
      'className' => 'Event', 
      'foreignKey' => 'event_id' 
     ), 
     'Score' => array(
      'className' => 'Score', 
      'foreignKey' => 'score_id' 
     ) 
); 
+1

古拉爵無限,sapevo車doveva essere未dettaglio重要提示哲非avevo considerato! 何cambiato IL諾姆刪除文件來MI海consigliatoËTUTTO公頃rispreso一個funzionare。在效率和效率方面1.3。 Ancora Grazie! 非常感謝,我知道它是一個重要的,沒有考慮細節。 我改變了文件名,正如你所建議的,它的工作。我仍然使用1.3版本的約定。 再次感謝! – Radj