2011-10-25 122 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']; 
    } 
} 

希望这有助于一路走来的人。