I wish I had a dollar for every time I have given this response!

I would use the Containable behaviour, which will give you really fine control 
over what data you get back. It effectively replaces 'recursive'. The problem 
with recursive is that it gives you all data within n steps of the current 
model, rather like rings on an onion. That can be too much data and doesn't 
offer much control.

In your app_model:

var $actsAs = array('Containable');
var $recursive = -1;

Now every model find will only bring back 'this' model, unless you specifically 
ask it to bring back more. Alternatively, you can place that code into selected 
models if you want to restrict its use, but once you get to grips with it 
you'll use it everywhere.

Now your find becomes:

$authors = $this->Author->find(
        'all',
        array(
                'contain' => array('Book')
        )
);

Then use the $authors variable as you already are. You should find that it now 
includes array keys for books belonging to each author.

If you want to bring back just one author:

$author = $this->Author->find(
        'first',
        array(
                'contain' => array('Book'),
                'conditions' => array('Author.id' => $id)
        )
);

(where $id is the id of the author you are searching for)

If books had publishers, you can bring their information back too:

$authors = $this->Author->find(
        'all',
        array(
                'contain' => array(
                        'Book' => array(
                                'Publisher'
                        )
                )
        )
);

You can see that by adding elements within the contain key you are extending 
your reach and getting back more data, but in a very controlled way.

I hope that helps.

Jeremy Burns
Class Outfit

[email protected]
http://www.classoutfit.com

On 12 Oct 2010, at 15:31, Ashwani Kumar wrote:

> Hi again! 
> 
> I'm getting some issue in getting associated array. as you know that when we 
> fetch some data from a model that's associated with another model via 
> $hasMany or $belongsTo, we also get data of that associated mode. But i'm not 
> getting data of that associated model.  I've two tables :
> authors
>        - id
>        - name
>        - email
>        - website
> 
> books
>        - id
>        - isbn
>        - title
>        - description
>        - author_id
> 
> When i fetch fetch authors data i dont' get data of books in result array. 
> 
> controllers/authors_controller.php
> 
> <?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
> 
> <?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
> 
> <?php
>        class Author extends AppModel {
>                var $name = 'Author';
>                var $hasMany = array('Book');
>        }
> 
> models/books.php
> 
> <?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        [email protected]       
> http://www.samspublications.com
> 2       Test Author             [email protected]          
> http://www.author1.com
> 
> 
> 
> 
> When i try to run print '<pre>'; print_r($authors), i get following
> array :
> 
> Array
> (
>    [0] => Array
>        (
>            [Author] => Array
>                (
>                    [id] => 1
>                    [name] => Sams Publication
>                    [email] => [email protected]
>                    [website] => http://www.samspublications.com
>                )
> 
>        )
> 
>    [1] => Array
>        (
>            [Author] => Array
>                (
>                    [id] => 2
>                    [name] => Test Author
>                    [email] => [email protected]
>                    [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 [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

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 [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