Re: Model association problem - not getting expected array using $hasMany and $belongsTo association

2010-10-12 Thread cricket
Everything appears to be correct. Try deleting any files in
app/tmp/cache/models/ if you haven't already. And set debug to 2, both
so the models won't be cached and so you can see what queries are
being run.

On Mon, Oct 11, 2010 at 2:04 PM, Ashwani Kumar
 wrote:
> Hi all! Today was trying to learn associations but i couldn't succeed.
> I'm getting some issue in getting associated array. I've two tables :
> authors
>        - id
>        - name
>        - email
>        - website
>
> books
>        - id
>        - isbn
>        - title
>        - description
>        - author_id
>
> controllers/authors_controller.php
>
>         class AuthorsController extends AppController {
>                var $name = 'Authors';
>
>                function index() {
>                        $this->Author->recursive = 1;
>                        $authors = $this->Author->find('all');
>                        $this->set('authors', $authors);
>                }
>        }
>
> controllers/books_controllers.php
>
>         class BooksController extends AppController {
>                var $name = 'Books';
>
>                function index() {
>                        $this->Book->recursive = 1;
>                        $books = $this->Book->find('all');
>                        $this->set('books', $books);
>                }
>        }
>
> models/authors.php
>
>         class Author extends AppModel {
>                var $name = 'Author';
>                var $hasMany = array('Book');
>        }
>
> models/books.php
>
>         class Book extends AppModel {
>                var $name = 'Book';
>                var $belongsTo = array('Author');
>        }
>
> books
> id      isbn    title           description             author_id
> 1       12345   book1           asdfasdfasdf                    1
> 2       book2   asdfasfasdf ag as sadfas dfsdaf         1
> 3       345345  dfgvsdf         gsdgsdf sdfg sdfg dfg   2
>
> authors
>
> id      name                            email                                 
>           website
> 1       Sams Publication        s...@samspublications.com       
> http://www.samspublications.com
> 2       Test Author             auth...@testauthors.com          
> http://www.author1.com
>
>
>
>
> When i try to run print ''; print_r($authors), i get following
> array :
>
> Array
> (
>    [0] => Array
>        (
>            [Author] => Array
>                (
>                    [id] => 1
>                    [name] => Sams Publication
>                    [email] => s...@samspublications.com
>                    [website] => http://www.samspublications.com
>                )
>
>        )
>
>    [1] => Array
>        (
>            [Author] => Array
>                (
>                    [id] => 2
>                    [name] => Test Author
>                    [email] => auth...@testauthors.com
>                    [website] => http://www.author1.com
>                )
>
>        )
>
> )
>
> So, my problem is that there's no Book array in above array. Please
> help... Thanks in advance.
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
>
> 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
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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


Model association problem - not getting expected array using $hasMany and $belongsTo association

2010-10-11 Thread Ashwani Kumar
Hi all! Today was trying to learn associations but i couldn't succeed.
I'm getting some issue in getting associated array. I've two tables :
authors
- id
- name
- email
- website

books
- id
- isbn
- title
- description
- author_id

controllers/authors_controller.php

Author->recursive = 1;
$authors = $this->Author->find('all');
$this->set('authors', $authors);
}
}

controllers/books_controllers.php

Book->recursive = 1;
$books = $this->Book->find('all');
$this->set('books', $books);
}
}

models/authors.php

http://www.samspublications.com
2   Test Author auth...@testauthors.com  
http://www.author1.com




When i try to run print ''; print_r($authors), i get following
array :

Array
(
[0] => Array
(
[Author] => Array
(
[id] => 1
[name] => Sams Publication
[email] => s...@samspublications.com
[website] => http://www.samspublications.com
)

)

[1] => Array
(
[Author] => Array
(
[id] => 2
[name] => Test Author
[email] => auth...@testauthors.com
[website] => http://www.author1.com
)

)

)

So, my problem is that there's no Book array in above array. Please
help... Thanks in advance.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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