我有一個場景,其中有許多主題的模板。 不夠清楚,我的關係將是模板的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
請告訴我這樣做的正確方法。
問題是不計算主題。問題是獲取每個模板的主題。模板一有2個主題和模板第二個有1個主題它顯示了第一個模板中的所有三個主題。感謝您的注意mentes .. –
試試這個'$ this-> Template-> bindModel( array( 'hasMany'=> array( 'TemplateTheme'=> array( 'className'=>'TemplateTheme', 'fields'=> array(' count(TemplateTheme.id)AS themes','another_field' ) ) ),false )' – mentes
問題是不計算mentes問題得到正確的數據 –