HABTM Select question

2009-03-21 Thread Hernan

Hi there,
I'm trying to make a (kind of) dictionary and I have the following
setup:

Tables:
tag: The word, for example, free
tag_content: Each tag has one or many contents or meanings: as in
beer and as in speech. This example might not be the best but those
contents could be linked to other tags.
tags_tag_content: The relationship table, since many tags can have
many contents and viceversa.

I have established the HABTM relationship correctly (Or so I think)

Then, in the Tag model, I have a function that should bring the all
contents for that tag.
How do I do that? is it possible using find or do I need to make a
custom query?

If I do a find on the Content model, I can bring all the contents but
I don't know how to do a contidion for Tag.id=1

I've also tried doing a find on the relationship table but it repeats
the Tag object in every returned row.

Thanks!



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: HABTM Select question

2009-03-21 Thread mscdex

On Mar 21, 8:07 pm, Hernan hernancalabr...@gmail.com wrote:
 Then, in the Tag model, I have a function that should bring the all
 contents for that tag.
 How do I do that? is it possible using find or do I need to make a
 custom query?

Assuming you have your models and everything set up correctly, you
should be able to use the model's find method to get what you want.

 If I do a find on the Content model, I can bring all the contents but
 I don't know how to do a contidion for Tag.id=1

Try something like: $tag = $this-Tag-find('first', array
('conditions' = array('Tag.id' = 1), 'recursive' = 0));
Then $tag should contain information about the tag and its associated
contents. You may wish to specify a 'fields' sub-array also if you do
not want to pull in all information from each model.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: HABTM Select question

2009-03-21 Thread Hernan

Thanks for the quick reply!
It does indeed work that way so thanks for that! However, I forgot to
tell a part of the story.

Those tags have other HATBM relationships as well to a 'users' table.
Many Users can have many tags. So when I execute the query, all the
users relationships come as well which is not cool. Is there any way
to tell which HATBM relationship to show and which not?

Thanks!

On Mar 21, 11:19 pm, mscdex msc...@gmail.com wrote:
 On Mar 21, 8:07 pm, Hernan hernancalabr...@gmail.com wrote:

  Then, in the Tag model, I have a function that should bring the all
  contents for that tag.
  How do I do that? is it possible using find or do I need to make a
  custom query?

 Assuming you have your models and everything set up correctly, you
 should be able to use the model's find method to get what you want.

  If I do a find on the Content model, I can bring all the contents but
  I don't know how to do a contidion for Tag.id=1

 Try something like: $tag = $this-Tag-find('first', array
 ('conditions' = array('Tag.id' = 1), 'recursive' = 0));
 Then $tag should contain information about the tag and its associated
 contents. You may wish to specify a 'fields' sub-array also if you do
 not want to pull in all information from each model.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: HABTM Select question

2009-03-21 Thread mscdex

On Mar 22, 12:04 am, Hernan hernancalabr...@gmail.com wrote:
 Is there any way to tell which HATBM relationship to show and which not?

Yup, use the Containable behavior in the Tag model. Then try something
like:
$tag = $this-Tag-find('first', array('conditions' = array('Tag.id'
= 1), 'contain' = array('TagContent')));
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: HABTM Select question

2009-03-21 Thread Hernan

Awesome! Thanks!

On Mar 22, 2:36 am, mscdex msc...@gmail.com wrote:
 On Mar 22, 12:04 am, Hernan hernancalabr...@gmail.com wrote:

  Is there any way to tell which HATBM relationship to show and which not?

 Yup, use the Containable behavior in the Tag model. Then try something
 like:
 $tag = $this-Tag-find('first', array('conditions' = array('Tag.id'
 = 1), 'contain' = array('TagContent')));
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
CakePHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---