我是cakephp2的新手,我开始寻找解决方案。问题是,我在cakephp2中有这样一个链接列表
但问题是,我想显示内容的数量,如下面的示例
对于像我这样一个成熟的人来说,这有点困难,我不知道如何在链接中实现列表或内容的数量,或者如何调用这个.It,如果像你这样的专业人士能帮助我的话,那就太棒了!为我糟糕的英语感到抱歉。
发布于 2014-10-30 20:18:52
您可能需要像这样的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>
希望这能有所帮助。
https://stackoverflow.com/questions/26624356
复制相似问题