The query:

$records = $this->Author->findAll();

will return nested associative arrays in the form:

Array
(
    [0] => Array
        (
            [Author] => Array
                (
                    ...// Fields from the Author model
                )
            [Book] => Array
                (
                    [0] => Array
                        (
                            .. // Fields from the Book model.

                            [AuthorsBooks] => Array
                                (
                                    ..// The join Model information
                                )

                        )
                    [1] => Array
                        (
                            .. // Fields from the Book model.

                            [AuthorsBooks] => Array
                                (
                                    ..// The join Model information
                                )

                        )
                )
        )
)

etc.

From the above structure (which you can view by calling
debug($records) ), and the article I linked earlier on the
Set::extract methods, you should be able to massage
the data into whatever logical form you desire.

Hope this helps.

-Joel.


On Apr 9, 11:31 am, inma <[EMAIL PROTECTED]> wrote:
> Thanks for your replies!
>
> martin, if you have this tables:
> CREATE TABLE `authors` (
>   `id` int(11) NOT NULL auto_increment,
>   `name` varchar(255) NOT NULL
>   PRIMARY KEY  (`id`)
> );
>
> CREATE TABLE `books` (
>   `id` int(11) NOT NULL auto_increment,
>   `name` varchar(255) NOT NULL,
>   `price` float(8,2) NOT NULL
>   PRIMARY KEY  (`id`)
> );
>
> CREATE TABLE `authors_books` (
>   `id` int(11) NOT NULL auto_increment,
>   `author_id` int(11) NOT NULL,
>   `book_id` int(11) NOT NULL,
>   PRIMARY KEY  (`id`)
> );
>
> And this relation:
>
> class Author extends AppModel
> {
>         var $name = 'Author';
>         var $hasAndBelongsToMany = array('Book');
>
> }
>
> class Book extends AppModel
> {
>         var $name = 'Book';
>         var $hasAndBelongsToMany = array('Author');
>
> }
>
> How do you display a report with all authors and their books?
>
> author.id   ||   author.name  ||   book.name  ||    book.price
>
> I know it is a basic question, but I don't know how to do it
> correctly...
>
> Thanks.
>
> On 9 abr, 17:00, "[EMAIL PROTECTED]"
>
> <[EMAIL PROTECTED]> wrote:
> > The usual and unhelpful answer applies:
> > You will have to rearrange your array-data yourself if you need it in
> > that format.
>
> > I would take another look at the javascript-component you are using...
> > because I can't really figure out why it would require the data in
> > that format. Repeating the user-data for each group sounds strange.
>
> > So if you really want to rearrange the data I would prefer to do it in
> > the User Model. You could create a method called "getUsersForExtJS()"
> > or something and in that method you do the actual findAll and some
> > data-manipulation before returning the data to the controller.
>
> > I would not use afterFind in this case since I really think it is only
> > something you want to do for some specific JS-component and not every
> > time you find a user.
>
> > On Apr 9, 4:28 pm, inma <[EMAIL PROTECTED]> wrote:
>
> > > Hi,
>
> > > I have an User class and a PermissionGroup class. User has defined a
> > > hasAndBelongsToMany relation with PermissionGroup.
>
> > > If into database exists one user with two permission grups assigned,
> > > when I run this code:
>
> > > $userA = $this->User->findAll($sort, $limit, $page); //gets all the
> > > User records and sorts them.
>
> > > It returns an array that contains the user data array and the
> > > permission groups array:
>
> > > Array(
> > >   [User] => Array(
> > >                     here the user data
> > >               )
> > >   [PermissionGroup] => Array(
> > >                    [0] => Array(
> > >                here the FIRST permission data
> > >                           )
> > >                    [1] => Array(
> > >                              here the SECOND permission data
> > >                           )
> > >                 )
> > > )
>
> > > I turn it into a json format because I use ExtJS library into the
> > > views:
>
> > > {"total":1, "users":[{"User":{ here the user data },
> > > "PermissionGroup":
> > > [{ here the first permission data }, { here the second permission
> > > data }]}]}
>
> > > But I need an array for each permission group assigned to user. Like
> > > this:
>
> > > Array(
> > >   [User] => Array(
> > >                                here the user data
> > >               )
> > >   [PermissionGroup] => Array(
> > >                    [0] => Array(
> > >                here the FIRST permission data
> > >                           )
> > >                 )
> > > ),
> > > Array(
> > >   [User] => Array(
> > >                                here the user data
> > >               )
> > >   [PermissionGroup] => Array(
> > >                    [0] => Array(
> > >                here the SECOND permission data
> > >                           )
> > >                 )
> > > )
>
> > > Turned into json format:
>
> > > {"total":2,
> > > "users":[{"User":{ here the user data }, "PermissionGroup":{ here the
> > > FIRST permission data }},
> > > {"User":{ here the user data }, "PermissionGroup":{ here the SECOND
> > > permission data }}
> > > ]}
>
> > > ¿How can I do it?
>
> > > Thanks in advance!
>
> > > P.S: ( I have asked this questions a few hours ago, but it doesn't
> > > appear. So I have repeated it...)- Ocultar texto de la cita -
>
> > - Mostrar texto de la cita -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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