You should perform a find in the Tag model, not in the news model,
like:
$taggedNews = $this->Tag->find('all', array(
'conditions' => array( 'Tag.name' => [your tag value here] ),
'recursive' => 1 ) );
Enjoy,
John
On Apr 16, 12:55 am, Cepheus <[email protected]> wrote:
> Hey everyone,
>
> I've setup a simple news page and I'm trying to add tags to my news
> posts. I've read a couple of tutorials on it and I've got the HABTM
> relationship working.
> My problem is that I'm not sure how to correctly query for news items
> that belong to a specific tag. I'm currently doing something like
> this:
>
> function index()
> {
> $news = $this->News->find('all');
>
> if (isset($this->params['requested']))
> {
> return $news;
> }
> else
> {
> $this->set('news', $news);
> }
> }
>
> Which gives me all news items as below:
>
> Array
> (
> [0] => Array
> (
> [News] => Array
> (
> [id] => 1
> [title] => Hello world!
> [author] => Michael
> [content] =>
>
> This is the first post!
>
> [created] => 2009-04-09 12:49:28
> [modified] => 2009-04-15 23:38:23
> )
>
> [Tag] => Array
> (
> [0] => Array
> (
> [id] => 3
> [name] => Profile
> [NewsTag] => Array
> (
> [id] => 1
> [news_id] => 1
> [tag_id] => 3
> )
>
> )
>
> )
>
> )
>
> [1] => Array
> (
> [News] => Array
> (
> [id] => 2
> [title] => Another newspost
> [author] => Michael
> [content] => Lorem ipsum dolor sit amet,
> consectetur adipiscing elit. Proin pellentesque dictum metus. Donec
> consectetur, nisi ac mollis pulvinar, urna enim semper
> [created] => 2009-04-09 18:29:32
> [modified] => 2009-04-09 18:29:32
> )
>
> [Tag] => Array
> (
> )
>
> )
>
> ... and so on.
>
> I tried rewriting it to just display news items that belong to the tag
> with id 3:
>
> function index()
> {
> $news = $this->News->find(array('NewsTag.tag_id'=>3));
>
> if (isset($this->params['requested']))
> {
> return $news;
> }
> else
> {
> $this->set('news', $news);
> }
> }
>
> But this does not work, instead it gives me this error:
>
> Warning (512): SQL Error: 1054: Unknown column 'NewsTag.tag_id' in
> 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line
> 525]
>
> Does anyone know what I can do to fix this problem?
>
> Thanks in advance,
> Michael.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---