Btw.... is the findAllByAuthorId intented to be pseudo-code, or is
this really possible in CakePHP?

On Oct 18, 11:55 am, grigri <[EMAIL PROTECTED]> wrote:
> I'm not 100% sure of the "official" way of doing this, but I'd do it
> by putting a specialized search function in the model. Which model is
> up to you - a bit of a grey area. I've written a quick (untested!)
> example, in which I've put it in the Author class. Depending on other
> stuff, it might make more sense for you to have a
> "findAllCategorizedByAuthor" function in the Book model, or something.
> Anyway:
>
> class Author extends AppModel {
>   var $name = "Author";
>
>   var $hasMany = array(
>     'Book'
>   );
>
>   function findCategorizedBooks($author_id) {
>     // First get all the books
>     // Order them by whatever you like, add extra conditions, ...
>     // Set recursive to -1 - we'll get the categories later
>     $books = $this->Book->findAllByAuthorId($author_id);
>
>     // Build a lookup table of category ids to book indices
>     $catLUT = array();
>     foreach ($books as $bookIdx => $book) {
>       $catId = $book['Book']['category_id'];
>       if (!isset($catLUT[$catId])) {
>         $catLUT[$catId] = array();
>       }
>
>       array_push($catLUT[$catId], $bookIdx);
>     }
>     // Grab the IDs for the findAll
>     $catIds = array_keys($catLUT);
>
>     // Find all the relevant categories
>     // Again, set recursive to -1 because we've already for the books
> and the author
>     // Order by whatever you like, add extra conditions, bla bla woof
> woof
>     $cats = $this->Book->Category->findAllById($catIds);
>
>     // Now loop through the category list and append the books
>     foreach ($cats as $catIdx => $cat) {
>       $cats[$catIdx]['Book'] = array();
>       foreach ($catLUT[$cat['Category']['id']] as $bookIdx) {
>         array_push($cats[$catIdx]['Book'], $books[$bookIdx]['Book']);
>       }
>     }
>
>     // We're done
>     return $cats;
>   }
>
> }
>
> Like I said, not tested, but I have written similar code before on a
> cake project of mine - the concept is sound.
> (Formatted a bit better here:http://openpaste.org/en/3506/)
>
> On Oct 18, 10:06 am, BoSc <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > I've got 3 tables with a 1:n relationship:
>
> > - author (has many books)
> > ^
> > |
> > |
> > V
> > - books (belongs to categories) (has many authors)
>
> >  |
> >  |
> > V
>
> > - categories (has many books)
>
> > What I would like to do on one page, is show a categorized view of all
> > books by a specific author. The problem lies in the fact that each
> > category and each book also has some kind of sorting involved, which
> > states at what place in a category a book should be shown...
>
> > >From which Point of View should I query this, and am I able to
>
> > implement this in 1 query?
>
> > What I would like to end up with is the following array
>
> > array
> >    category 2
> >       book 1
> >          name
> >          blabla
> >       book 3
> >          name
> >          blabla
> >       book 2
> >          name
> >          blabla
> >    category 1
> >       book 4
> >          name
> >          blabla
> >       book 6
> >          name
> >          blabla
> >       book 5
> >          name
> >          blabla
>
> > The problem is, when I use findAll on Category, I'm not able to choose
> > how Cake should order the Books. Any workarounds?
>
> > Thanks


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to