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 would
be ....... $similarBooks = $this->find('all')............ and just
return your results).

Then call this model's function from wherever you need it, whether it
be the genres_controller:

$this->Genre->Book->similar($id);

... or your books_controller....

$this->Book->similar($id);

.... or even from a completely unrelated model (use sparingly)....

$Book = ClassRegistry::init('Book');
$similar = $Book->similar();

It's possible that I'm missing the actual issue, but two important
points: 1) never use requestAction (as you probably know), and 2)
there should never be any variables in your views that aren't
available in your controllers, because there shouldn't be any new
variables assigned. If this is ever required, you're likely in need of
a Helper class. And yes, it would be worth looking at creating
elements for anything like "Similar books", or "5 Related Authors".
You can pass variables into elements.

I hope this helps.

On Apr 11, 5:00 pm, Mike Cook <m...@epubbooks.com> wrote:
> 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 similar($id = null) {
>     $similarbooks = $this->Genre->Book->find('all', array(
>       'contain' => array(
>         'Author' => array('fields' => array('id', 'slug')),
>       ),
>       'fields' => array('id', 'slug', 'title'),
>       'limit' => 5,
>       'order' => 'RAND()',
>     ));
>     $this->set(compact('similarbooks'));
>   }
>
> // Book view.ctp
>   $results = $this->requestAction('/genres/similar/' . $id);
>
> Of course I don't want to really use requestAction (the only place so
> far I have used it!) but I would also like to be able to bring up 5
> random books from ALL the genres the book is assigned to - at the
> moment I am just searching on one genre id.
>
> I also know that this approach is not very good if I wish to include 5
> books which are related to say, both Genre and Author.
>
> At some point down the line I would also like to include a "5 Related
> Authors" list, so I guess I need a more modular approach (would
> elements be it?) - the problem is that I am completely stumped, even
> after a week looking at this!
>
> The solution I currently have is better than nothing, but I would be
> very grateful if anyone has an idea on how I could achieve a proper
> solution.
>
> Many thanks, Mike.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to