2011-10-25 121 views
0

我有一個場景,其中有許多主題的模板。 不夠清楚,我的關係將是模板的hasMany主題 我想告訴與主題數的模板,我將這段代碼:如何正確地關聯模型cakephp

$this->Template->recursive = 2; 
    $this->Template->bindModel(
         array(
          'hasMany' =>array(
          'TemplateTheme'=>array(
           'className'=>'TemplateTheme', 
           'fields' => 'count(TemplateTheme.id) AS themes' 
          ) 
         ) 
        ),false 
    ); 
    $this->paginate = array(
         'order' => array('Template.modified DESC'), 
         'limit' =>$limit 
        ); 
    $template = $this->paginate('Template'); 
    pr($template);die(); 

,但我得到的是

Array 
(
    [0] => Array 
     (
      [Template] => Array 
       (
        [id] => 1 
        [name] => churchDesign 
        [status] => Active 
        [created] => 2011-10-24 10:37:23 
        [modified] => 2011-10-25 15:16:46 
       ) 

      [TemplateTheme] => Array 
       (
        [0] => Array 
         (
          [template_id] => 1 
          [TemplateTheme] => Array 
           (
            [0] => Array 
             (
              [themes] => 3 
             ) 

           ) 

         ) 

       ) 

     ) 

    [1] => Array 
     (
      [Template] => Array 
       (
        [id] => 2 
        [name] => blossoms 
        [status] => Active 
        [created] => 2011-10-19 00:00:00 
        [modified] => 2011-10-24 14:05:27 
       ) 

      [TemplateTheme] => Array 
       (
       ) 

     ) 

) 

的這裏的問題是計數第一個數組(第一個模板)中的所有主題。參見[TemplateTheme] => Array ( [0] => Array ( [主題] => 3 ) 第三個主題實際上屬於第二個模板。關係爲

template_id位於主題表格中以表示模板的每個主題。

模板1有2個主題和模板2有一個主題,目前它顯示了第一個模板中的所有三個主題。

我希望它像這樣

templatename1 
count of themes 1st template 

template name 2 
count of themes of 2nd template 

請告訴我這樣做的正確方法。

回答

0

可以算的主題陣列狀

foreach($template as $t){ 
    count($t['TemplateTheme']) 
} 

希望這有助於

+0

問題是不計算主題。問題是獲取每個模板的主題。模板一有2個主題和模板第二個有1個主題它顯示了第一個模板中的所有三個主題。感謝您的注意mentes .. –

+0

試試這個'$ this-> Template-> bindModel( array( 'hasMany'=> array( 'TemplateTheme'=> array( 'className'=>'TemplateTheme', 'fields'=> array(' count(TemplateTheme.id)AS themes','another_field' ) ) ),false )' – mentes

+0

問題是不計算mentes問題得到正確的數據 –

0

你應該使用find(「計數」)方法而不是使用虛擬「計數」現場像你現在做。例如:

$allTemplates = $this->Template->find('all'); 
foreach($allTemplates as $data) { 
    $templateCount = $this->Template->TemplateTheme->find('count', array(
     'conditions' => array(
      'TemplateTheme.template_id' => $data['Template']['id'] 
     ) 
    )); 
    // $templateCount should now contain the count of this specific template. Foreach will continue recursion for all others. 
} 
+0

沒有像加入自定義連接一樣加入模型的方法嗎? –

1

如果你想獲得每每個模板的主題的計數,最優雅的方式是使用counterCache

另一種方法是使用Containable行爲,該行爲僅獲取您希望的相關數據,然後在PHP中對獲取的數據數組進行計數。 後您將附加中可容納的行爲模板模型,像這樣的代碼應該工作:

$allTemplates = $this->Templates->find('all', array(
         'contain' => 'TemplateTheme.name' 
       )); 
foreach($allTemplates as $template){ 
    $currentThemeCount = count($template['TemplateTheme']); 
    foreach($template['TemplateTheme'] as $theme){ 
     echo $theme['name']; 
    } 
} 

希望這有助於一路走來的人。