I'm new to cakephp2 and I came to be find some solution. The problem is that I have a list of links in cakephp2 like this
But the problem is that I want to display the number of contents like the sample below
For a amature like me it is a bit too difficult.I do not know how to implement the number of lists or contents in the link or how to call this .It would be great if the pros like you can help me out! Sorry for my poor English.
You will probably need a HABTM setup like this:
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',
)
);
}
Add following to your AppModel, you will never look back:
class AppModel extends Model {
public $actsAs = array('Containable');
}
In controller:
$results = $this->Tag->find('all, array(
'contain' => array('Article)
));
$this->set('results', $results);
In view:
<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>
Hope this helps.