2014-10-29 44 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> 

希望这会有所帮助。