2014-10-29 42 views
0

我是cakephp2的新手,我來找到一些解決方案。 的問題是,我有一個鏈接列表中cakephp2這樣Cakephp2:如何顯示cakephp2中的鏈接內容的數量

但問題是,我想以顯示如下

的取樣類似的內容的數量對於像我這樣的小孩來說,這有點太難了。我不知道如何在鏈接中實現列表或內容的數量,或者如何調用它。如果像你這樣的專業人員能幫助我,這將是非常棒的! 對不起,我英文很差。

+1

SO並不是人們爲你做所有工作的地方,而是幫助解決特定問題。所以,如果你需要幫助,那麼你應該指定你正面臨的實際編程相關問題。 ** http://stackoverflow.com/help/how-to-ask** – ndm 2014-10-29 13:00:51

+0

對不起。作爲一名初中生,我認爲最好先向經驗豐富的程序員請教,因爲我厭倦了將蹩腳的網站作爲愛好。但我很感激幫助我的人。 – user3264924 2014-10-31 09:38:14

回答

1

你可能會需要一個HABTM設置是這樣的:

Class Tag extends AppModel { 

    public $name = 'Tag'; 

    public $hasAndBelongsToMany = array(
     'Article' => array(
      'className' => 'Article', 
      'joinTable' => 'articles_tags', 
      'foreignKey' => 'tag_id', 
      'associationForeignKey' => 'article_id', 
      ) 
     ); 
} 

Class ArticlesTag extends AppModel { 

    public $name = 'ArticlesTag'; 
} 

Class Article extends AppModel { 

    public $name = 'Article'; 

    public $hasAndBelongsToMany = array(
     'Tag' => array(
      'className' => 'Tag', 
      'joinTable' => 'articles_tags', 
      'foreignKey' => 'article_id', 
      'associationForeignKey' => 'tag_id', 
      ) 
     ); 
} 

添加下列內容AppModel,你將永遠不會回頭:

class AppModel extends Model { 

    public $actsAs = array('Containable'); 
} 

在控制器:

$results = $this->Tag->find('all, array(
    'contain' => array('Article) 
    )); 

$this->set('results', $results); 

鑑於:

<ul class="tags"> 

<?php foreach ($results as $data) : ?> 
<li class="tag"><?php 

echo $this->Html->link($data['Tag']['name'] . ' (' . count($data['Article']) . ')', 
    '/tag/' . $data['Tag']['slug'], 
    array('title' => 'View articles for ' . $data['Tag']['name'], 'escape' => false)); 

?></li> 
<?php endforeach; ?> 

</ul> 

希望這會有所幫助。