Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-17 Thread Mike Cook
Okay, so I just couldn't leave this alone :) I implemented the following, which works and even brings in the 'contain' info. However there is still a small problem... // books_controller.php view() $similar = $this->Book->similar_books($genre_id, $limit); // model book.php $similarBooks = $this

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-14 Thread Mike Cook
Thanks brian. I checked those out and after a few attempts I got it workingthat is until I tried to add a 'contain'. If I try to bring in some more data with the contain then the whole thing returns nothing. If I comment out the 'group' then I get data again, although this is the wrong data.

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-13 Thread brian
I didn't realise it was HABTM. That changes things. See these posts: http://teknoid.wordpress.com/2008/08/06/habtm-and-join-trickery-with-cakephp/ http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find On Mon, Apr 13, 2009 at 4:48 AM, Mike Cook wrote: > > brian, when

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-13 Thread Mike Cook
brian, when I try this though I get an " Unknown column 'Book.genre_id' in 'where clause' " error. SELECT `Book`.`id`, `Book`.`title`, `Book`.`slug`, `Language`.`id`, `Language`.`name` FROM `books` AS `Book` LEFT JOIN `languages` AS `Language` ON (`Book`.`language_id` = `Language`.`id`) WHERE `Bo

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-12 Thread brian
I'd also call the function genre() in case I ever wanted to add a broader similar() functionality. On Sun, Apr 12, 2009 at 3:25 PM, brian wrote: > There's no need to search on Genre as you have the foreign key already. > > function similar($genre_id) > { >        return $this->find( >          

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-12 Thread brian
There's no need to search on Genre as you have the foreign key already. function similar($genre_id) { return $this->find( 'all', array( 'fields' => array( 'Book.id', 'Bo

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-12 Thread Mike Cook
Okay, so after more searching around I found a post by pkclarke who makes use of a search on the join table, and then uses a find() with those results. I still have the feeling this is not the best way it should be done, but I think this approach will allow me to move forwardand no requestAct

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-11 Thread Mike Cook
Hi Joe, During my experimentations I did come up with something similar, but one of the problems is how to set the conditions to work on the Genre ID. So, I have a book that has two genre, "Mystery" (id=5) and "Short Story" (id=20) and if I only try to search with say, "Mystery"; $id = 5; $this

Re: Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-11 Thread Joe Critchley
You've currently got your similar() function in your genres_controller, whereas the primary model for the query is the Book model (as it's finding related *books*, not genres). I believe the following would be a more scalable approach. Place your similar() function into your Book model. (So it w

Getting "5 Related" Books via multiple Genres (HABTM ) from within Book view() and without requestAction

2009-04-11 Thread Mike Cook
In my books view.ctp, I want to have a list of 5 books that are related via the genres data, which has a HABTM relationship. Although I can do this by placing a function similar() in the genres_controller, I have to call it from the view with requestAction. // genres_controller.php function sim