Re: Tagging system in Cake - hasAndBelongsToMany problems
Thanks for the input I've tried everything suggested, and none of them work. The strange thing is my save/create/update behaviour is working, so Tags and Games are getting created and linked properly during updates & deletes - I infer from this that my associations & models are set up OK. With the debug level set to 3 I can see that cake does two queries rather than an actual SQL JOIN, e.g. SELECT `Tag`.`id` FROM `tags` AS `Tag` WHERE `Tag`.`name` = 'foo' LIMIT 1 Followed by SELECT blahblah FROM `games` AS `Game` JOIN `games_tags` AS `GamesTag` ON (`GamesTag`.`tag_id` = 13 AND `GamesTag`.`game_id` = `Game`.`id`) Which seems a very odd way of going about things... In the end I've used my own SQL statement using the query() function where I have proper control over the joins.- this is the only time in using cake I've had to resort to this. Oh well Thanks again. On Apr 4, 11:07 am, Ben Coleman wrote: > Hi, > > I've been using Cake for some time, but recently hit a problem with > hasAndBelongsToMany. > I have a videogame database, I'm trying to add a tagging system to it. > I'm storing my tags in 3NF. > > So I have a model for games which is binding Games to Tags with > hasAndBelongsToMany and also my Tag model binds back to Games with a > hasAndBelongsToMany. The Games model is also bound to some other > tables with belongsTo (e.g. Genre, Region, System etc,). Naturally I > have a join table called "games_tags" linking the many-to-many > relationship. > > I want to query my system and get all games with a specific tag, and > this is where it falls down > If I do: > $this->Game->Tag->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > It works but it doesn't join the other tables to Game, so I can't get > the Game's genre or system > $this->Tag->Game->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > This fails with an error "Unknown column 'Tag.name' in 'where clause" > > Either way it's not joining all the tables properly, it seems to do > two separate queries > This is so frustrating because if I was writing the SQL by hand I > would have this solved in a matter of minutes, but Cake removes any of > this freedom. > > What am I doing wrong??? this is such an obvious thing to do > > Thanks in advance > Ben --~--~-~--~~~---~--~~ 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: Tagging system in Cake - hasAndBelongsToMany problems
Use the containable behaviour. $this->Game->Tag->find('all', array('conditions' => array('Tag.name' => 'foo'), 'contain' => array('Game' => array('Genre', 'System')); As long as your associations are correctly configured it should work. Read up about it at http://book.cakephp.org/view/474/Containable On Apr 4, 12:07 pm, Ben Coleman wrote: > Hi, > > I've been using Cake for some time, but recently hit a problem with > hasAndBelongsToMany. > I have a videogame database, I'm trying to add a tagging system to it. > I'm storing my tags in 3NF. > > So I have a model for games which is binding Games to Tags with > hasAndBelongsToMany and also my Tag model binds back to Games with a > hasAndBelongsToMany. The Games model is also bound to some other > tables with belongsTo (e.g. Genre, Region, System etc,). Naturally I > have a join table called "games_tags" linking the many-to-many > relationship. > > I want to query my system and get all games with a specific tag, and > this is where it falls down > If I do: > $this->Game->Tag->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > It works but it doesn't join the other tables to Game, so I can't get > the Game's genre or system > $this->Tag->Game->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > This fails with an error "Unknown column 'Tag.name' in 'where clause" > > Either way it's not joining all the tables properly, it seems to do > two separate queries > This is so frustrating because if I was writing the SQL by hand I > would have this solved in a matter of minutes, but Cake removes any of > this freedom. > > What am I doing wrong??? this is such an obvious thing to do > > Thanks in advance > Ben --~--~-~--~~~---~--~~ 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: Tagging system in Cake - hasAndBelongsToMany problems
Have a look at this tip: http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find On Sat, Apr 4, 2009 at 6:07 AM, Ben Coleman wrote: > > Hi, > > I've been using Cake for some time, but recently hit a problem with > hasAndBelongsToMany. > I have a videogame database, I'm trying to add a tagging system to it. > I'm storing my tags in 3NF. > > So I have a model for games which is binding Games to Tags with > hasAndBelongsToMany and also my Tag model binds back to Games with a > hasAndBelongsToMany. The Games model is also bound to some other > tables with belongsTo (e.g. Genre, Region, System etc,). Naturally I > have a join table called "games_tags" linking the many-to-many > relationship. > > I want to query my system and get all games with a specific tag, and > this is where it falls down > If I do: > $this->Game->Tag->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > It works but it doesn't join the other tables to Game, so I can't get > the Game's genre or system > $this->Tag->Game->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > This fails with an error "Unknown column 'Tag.name' in 'where clause" > > Either way it's not joining all the tables properly, it seems to do > two separate queries > This is so frustrating because if I was writing the SQL by hand I > would have this solved in a matter of minutes, but Cake removes any of > this freedom. > > What am I doing wrong??? this is such an obvious thing to do > > Thanks in advance > Ben > > > --~--~-~--~~~---~--~~ 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: Tagging system in Cake - hasAndBelongsToMany problems
Ben, Here is an example from the book - http://book.cakephp.org/en/view/83/hasAndBelongsToMany-HABTM -teh $this->Recipe->bindModel(array( 'hasOne' => array( 'RecipesTag', 'FilterTag' => array( 'className' => 'Tag', 'foreignKey' => false, 'conditions' => array('FilterTag.id = RecipesTag.tag_id') ; $this->Recipe->find('all', array( 'fields' => array('Recipe.*'), 'conditions'=>array('FilterTag.name'=>'Dessert') )); On Apr 4, 5:07 am, Ben Coleman wrote: > Hi, > > I've been using Cake for some time, but recently hit a problem with > hasAndBelongsToMany. > I have a videogame database, I'm trying to add a tagging system to it. > I'm storing my tags in 3NF. > > So I have a model for games which is binding Games to Tags with > hasAndBelongsToMany and also my Tag model binds back to Games with a > hasAndBelongsToMany. The Games model is also bound to some other > tables with belongsTo (e.g. Genre, Region, System etc,). Naturally I > have a join table called "games_tags" linking the many-to-many > relationship. > > I want to query my system and get all games with a specific tag, and > this is where it falls down > If I do: > $this->Game->Tag->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > It works but it doesn't join the other tables to Game, so I can't get > the Game's genre or system > $this->Tag->Game->find('all', array('conditions' => array('Tag.name > =' => 'foo'))); > This fails with an error "Unknown column 'Tag.name' in 'where clause" > > Either way it's not joining all the tables properly, it seems to do > two separate queries > This is so frustrating because if I was writing the SQL by hand I > would have this solved in a matter of minutes, but Cake removes any of > this freedom. > > What am I doing wrong??? this is such an obvious thing to do > > Thanks in advance > Ben --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Tagging system in Cake - hasAndBelongsToMany problems
Hi, I've been using Cake for some time, but recently hit a problem with hasAndBelongsToMany. I have a videogame database, I'm trying to add a tagging system to it. I'm storing my tags in 3NF. So I have a model for games which is binding Games to Tags with hasAndBelongsToMany and also my Tag model binds back to Games with a hasAndBelongsToMany. The Games model is also bound to some other tables with belongsTo (e.g. Genre, Region, System etc,). Naturally I have a join table called "games_tags" linking the many-to-many relationship. I want to query my system and get all games with a specific tag, and this is where it falls down If I do: $this->Game->Tag->find('all', array('conditions' => array('Tag.name =' => 'foo'))); It works but it doesn't join the other tables to Game, so I can't get the Game's genre or system $this->Tag->Game->find('all', array('conditions' => array('Tag.name =' => 'foo'))); This fails with an error "Unknown column 'Tag.name' in 'where clause" Either way it's not joining all the tables properly, it seems to do two separate queries This is so frustrating because if I was writing the SQL by hand I would have this solved in a matter of minutes, but Cake removes any of this freedom. What am I doing wrong??? this is such an obvious thing to do Thanks in advance Ben --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---