【问题标题】:Cakephp2: How to display number of contents within the link in the cakephp2Cakephp 2:如何在 cakephp 2 中显示链接内的内容数量
【发布时间】:2014-10-29 06:56:34
【问题描述】:

我是 cakephp2 的新手,我来寻找一些解决方案。 问题是我有一个像这样的 cakephp2 链接列表

但问题是我想像下面的示例那样显示内容的数量

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

【问题讨论】:

  • SO 不是人们为您完成所有工作的地方,而是帮助解决特定问题的地方。因此,如果您需要帮助,那么您应该指定您面临的实际编程相关问题。 stackoverflow.com/help/how-to-ask
  • 对不起。作为一名初中生,我认为最好先问有经验的程序员,因为我已经厌倦了将制作蹩脚的网站作为一种爱好。但我很感谢帮助我的人。

标签: php cakephp-2.0 tag-cloud


【解决方案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>

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多