Cakephp 2:如何在cakephp 2中显示链接中的内容数量

时间:2014-10-29 06:56:34

标签: php cakephp-2.0 tag-cloud

我是cakephp2的新手,我找到了一些解决方案。 问题是我在cakephp2中有像这样的链接列表

但问题是我想显示内容的数量,如下面的示例

对于像我这样的人来说,这有点太难了。我不知道如何实现链接中的列表或内容的数量,或者如何调用它。如果像你这样的专业人士可以帮助我,那将会很棒! 抱歉我的英语不好。

1 个答案:

答案 0 :(得分: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>

希望这有帮助。