Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-13 Thread Ashwani Kumar
Thanx jeremy, so much, for all the help you did for me. Sorry, I didn't
tried Containable, that you asked me to do earlier, because i've just
started learning cakephp and i want to walk step by step.
I got the error. It was my mistake that i named my model filename books.php
and authors.php. Cakephp was not able to find models and that's why the
association couldn't be made.
I'm already a big fan of yours and i'll if i've any question or query. I
also visited your website classoutfit.com, that's such a cool site you've
made. Thanx again for all your help.

On Tue, Oct 12, 2010 at 11:50 PM, Jeremy Burns | Class Outfit 
jeremybu...@classoutfit.com wrote:

 Remove $this-Book-recursive = 1;  That line is effectively stopping any
 searches pat the Book model.

 Did you follow my other tip about Containable?

 A tip; instead of doing pre pre etc... just do debug($authors);

 Jeremy Burns
 *Class Outfit*
 *
 *
 jeremybu...@classoutfit.com jeremybu...@mac.com
 (t) +44 (0) 208 123 3822
 (m) +44 (0) 7973 481949
 Skype: jeremy_burns
 http://www.classoutfit.com

 On 12 Oct 2010, at 18:28, Ashwani Kumar wrote:

 Hi Jeremy! I added
 $authors = $this-Book-Author-find('list');
  in books_controller and now books_controller looks like this :

 ?php
 class BooksController extends AppController {
 var $name = 'Books';

 function index() {
 $this-Book-recursive = 1;
 $books = $this-Book-find('all');
 $this-set('books', $books);
 $authors = $this-Book-Author-find('list');
 $this-set('authors', $authors);
 }
 }

 then i move on to views/books/index.ctp and wrote ?php print 'pre';
 print_r($authors);exit; ? It gave me following error :

 *Notice* (8): Undefined property: AppModel::$Author 
 [*APP\controllers\books_controller.php*, line *9*]

 Code | Context

 $books=   array(
   array(
   Book = array()
 ),
   array(
   Book = array()
 )
 )

 $books = $this-Book-find('all');$this-set('books', 
 $books);$author = $this-Book-Author-find('list');

 BooksController::index() - APP\controllers\books_controller.php, line 9
 Object::dispatchMethod() - CORE\cake\libs\object.php, line 116
 Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 227
 Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 194
 [main] - APP\webroot\index.php, line 88


 *Fatal error*: Call to a member function find() on a non-object in *
 C:\webs\test\relationship\app\controllers\books_controller.php* on line *9

 I think models couldn't get associated here. What do u think???



 *

 On Tue, Oct 12, 2010 at 10:40 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:

 In your add function in the books_controller, you need to populate the
 $authors variable:

 $authors = $this-Book-Author-find('list');

 Then make sure the variable is passed into the view:

 $this-set(compact('authors'));

 or

 $this-set('authors, $authors);

 (they both do the same thing - use the first version if you have more than
 one variable to set and pass)

 Then if you are using the Form helper to build your form, you should have
 this:

 echo $this-Form-input('author_id');

 When Cake draws the author_id input, it will check to see if there is a
 variable that matches (author_id = $authors, book_id = $books, anything_id
 = $anythings - you can follow the pattern). If it finds one (in your case
 $authors) it will assume you want author_id to be a select list, so will use
 the $authors variable as the options.

 You can embellish the input further by adding other options, e.g.:

 echo $this-Form-input(
 'author_id',
  array(
 'empty' = true
 )
 );

 The empty option means that no value is already selected in the list; by
 default the first option value (your first author) is pre-populated. You can
 also do:

 'empty' = 'Please choose an author'


 Jeremy Burns
 *Class Outfit*
 *
 *
 jeremybu...@classoutfit.com jeremybu...@mac.com
 http://www.classoutfit.com

 On 12 Oct 2010, at 17:59, Ashwani Kumar wrote:

 Hi jeremy! Thanx for the reply. I deleted all files from cakephp except
 authors_controller and books_controller and then i added var $scaffold in
 both the files and also deleted all functions from both files. Then i added
 some authors. Now at the time of adding books, Author field should be a Drop
 down menu. But its not happening in my case. Author field is just a Text
 field here in my case. If you could explain why this is happening? is this
 an issue with my cakephp version of what?? By the way i'm using CakePHP 1.3

 On Tue, Oct 12, 2010 at 8:13 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:

 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 

Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-13 Thread Jeremy Burns | Class Outfit
I'm blushing.

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 13 Oct 2010, at 16:38, Ashwani Kumar wrote:

 Thanx jeremy, so much, for all the help you did for me. Sorry, I didn't tried 
 Containable, that you asked me to do earlier, because i've just started 
 learning cakephp and i want to walk step by step. 
 I got the error. It was my mistake that i named my model filename books.php 
 and authors.php. Cakephp was not able to find models and that's why the 
 association couldn't be made. 
 I'm already a big fan of yours and i'll if i've any question or query. I also 
 visited your website classoutfit.com, that's such a cool site you've made. 
 Thanx again for all your help. 
 
 On Tue, Oct 12, 2010 at 11:50 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:
 Remove $this-Book-recursive = 1;  That line is effectively stopping any 
 searches pat the Book model.
 
 Did you follow my other tip about Containable?
 
 A tip; instead of doing pre pre etc... just do debug($authors);
 
 Jeremy Burns
 Class Outfit
 
 jeremybu...@classoutfit.com
 (t) +44 (0) 208 123 3822
 (m) +44 (0) 7973 481949
 Skype: jeremy_burns
 http://www.classoutfit.com
 
 On 12 Oct 2010, at 18:28, Ashwani Kumar wrote:
 
 Hi Jeremy! I added 
 $authors = $this-Book-Author-find('list');
  in books_controller and now books_controller looks like this : 
 
 ?php
 class BooksController extends AppController {
 var $name = 'Books';
 
 function index() {
 $this-Book-recursive = 1;
 $books = $this-Book-find('all');
 $this-set('books', $books);
 $authors = $this-Book-Author-find('list');
 $this-set('authors', $authors);
 }
 }
 
 then i move on to views/books/index.ctp and wrote ?php print 'pre'; 
 print_r($authors);exit; ? It gave me following error : 
 
 Notice (8): Undefined property: AppModel::$Author 
 [APP\controllers\books_controller.php, line 9]
 Code | Context
 $books   =   array(
  array(
  Book = array()
 ),
  array(
  Book = array()
 )
 )
 $books = $this-Book-find('all');
 
 $this-set('books', $books);
 
 $author = $this-Book-Author-find('list');
 
 BooksController::index() - APP\controllers\books_controller.php, line 9
 Object::dispatchMethod() - CORE\cake\libs\object.php, line 116
 Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 227
 Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 194
 [main] - APP\webroot\index.php, line 88
 
 Fatal error: Call to a member function find() on a non-object in 
 C:\webs\test\relationship\app\controllers\books_controller.php on line 9
 
 I think models couldn't get associated here. What do u think???
 
 
 
 
 On Tue, Oct 12, 2010 at 10:40 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:
 In your add function in the books_controller, you need to populate the 
 $authors variable:
 
 $authors = $this-Book-Author-find('list');
 
 Then make sure the variable is passed into the view:
 
 $this-set(compact('authors'));
 
 or
 
 $this-set('authors, $authors);
 
 (they both do the same thing - use the first version if you have more than 
 one variable to set and pass)
 
 Then if you are using the Form helper to build your form, you should have 
 this:
 
 echo $this-Form-input('author_id');
 
 When Cake draws the author_id input, it will check to see if there is a 
 variable that matches (author_id = $authors, book_id = $books, anything_id 
 = $anythings - you can follow the pattern). If it finds one (in your case 
 $authors) it will assume you want author_id to be a select list, so will use 
 the $authors variable as the options.
 
 You can embellish the input further by adding other options, e.g.:
 
 echo $this-Form-input(
  'author_id',
  array(
  'empty' = true 
  )
 );
 
 The empty option means that no value is already selected in the list; by 
 default the first option value (your first author) is pre-populated. You can 
 also do:
 
 'empty' = 'Please choose an author'
 
 
 Jeremy Burns
 Class Outfit
 
 jeremybu...@classoutfit.com
 http://www.classoutfit.com
 
 On 12 Oct 2010, at 17:59, Ashwani Kumar wrote:
 
 Hi jeremy! Thanx for the reply. I deleted all files from cakephp except 
 authors_controller and books_controller and then i added var $scaffold in 
 both the files and also deleted all functions from both files. Then i added 
 some authors. Now at the time of adding books, Author field should be a 
 Drop down menu. But its not happening in my case. Author field is just a 
 Text field here in my case. If you could explain why this is happening? is 
 this an issue with my cakephp version of what?? By the way i'm using 
 CakePHP 1.3 
 
 On Tue, Oct 12, 2010 at 8:13 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:
 I wish I had a dollar for every time I have given this response!
 
 I would use the Containable 

Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-12 Thread Ashwani Kumar
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  isbntitle   description author_id
1   12345   book1   asdfasdfasdf1
2   book2   asdfasfasdf ag as sadfas dfsdaf 1
3   345345  dfgvsdf gsdgsdf sdfg sdfg dfg   2

authors

id  nameemail
website
1   Sams Publications...@samspublications.com
http://www.samspublications.com
2   Test Author auth...@testauthors.com
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] = 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


Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-12 Thread Jeremy Burns | Class Outfit
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

jeremybu...@classoutfit.com
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  isbntitle   description author_id
 1   12345   book1   asdfasdfasdf1
 2   book2   asdfasfasdf ag as sadfas dfsdaf 1
 3   345345  dfgvsdf gsdgsdf sdfg sdfg dfg   2
 
 authors
 
 id  nameemail 
   website
 1   Sams Publications...@samspublications.com   
 http://www.samspublications.com
 2   Test Author auth...@testauthors.com  
 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] = 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 

Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-12 Thread Ashwani Kumar
Hi jeremy! Thanx for the reply. I deleted all files from cakephp except
authors_controller and books_controller and then i added var $scaffold in
both the files and also deleted all functions from both files. Then i added
some authors. Now at the time of adding books, Author field should be a Drop
down menu. But its not happening in my case. Author field is just a Text
field here in my case. If you could explain why this is happening? is this
an issue with my cakephp version of what?? By the way i'm using CakePHP 1.3

On Tue, Oct 12, 2010 at 8:13 PM, Jeremy Burns | Class Outfit 
jeremybu...@classoutfit.com wrote:

 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*
 *
 *
 jeremybu...@classoutfit.com jeremybu...@mac.com
 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  isbntitle   description author_id
 1   12345   book1   asdfasdfasdf1
 2   book2   asdfasfasdf ag as sadfas dfsdaf 1
 3   345345  dfgvsdf gsdgsdf sdfg sdfg dfg   2

 authors

 id  nameemail
 website
 1   Sams Publications...@samspublications.com
 http://www.samspublications.com
 2   Test Author auth...@testauthors.com
 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] = s...@samspublications.com
[website] = http://www.samspublications.com
)

)

[1] = Array
(
[Author] = Array
(
[id] = 2
[name] = Test Author
 

Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-12 Thread Jeremy Burns | Class Outfit
In your add function in the books_controller, you need to populate the $authors 
variable:

$authors = $this-Book-Author-find('list');

Then make sure the variable is passed into the view:

$this-set(compact('authors'));

or

$this-set('authors, $authors);

(they both do the same thing - use the first version if you have more than one 
variable to set and pass)

Then if you are using the Form helper to build your form, you should have this:

echo $this-Form-input('author_id');

When Cake draws the author_id input, it will check to see if there is a 
variable that matches (author_id = $authors, book_id = $books, anything_id = 
$anythings - you can follow the pattern). If it finds one (in your case 
$authors) it will assume you want author_id to be a select list, so will use 
the $authors variable as the options.

You can embellish the input further by adding other options, e.g.:

echo $this-Form-input(
'author_id',
array(
'empty' = true 
)
);

The empty option means that no value is already selected in the list; by 
default the first option value (your first author) is pre-populated. You can 
also do:

'empty' = 'Please choose an author'

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
http://www.classoutfit.com

On 12 Oct 2010, at 17:59, Ashwani Kumar wrote:

 Hi jeremy! Thanx for the reply. I deleted all files from cakephp except 
 authors_controller and books_controller and then i added var $scaffold in 
 both the files and also deleted all functions from both files. Then i added 
 some authors. Now at the time of adding books, Author field should be a Drop 
 down menu. But its not happening in my case. Author field is just a Text 
 field here in my case. If you could explain why this is happening? is this an 
 issue with my cakephp version of what?? By the way i'm using CakePHP 1.3 
 
 On Tue, Oct 12, 2010 at 8:13 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:
 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
 
 jeremybu...@classoutfit.com
 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() {

Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-12 Thread Ashwani Kumar
Hi Jeremy! I added
$authors = $this-Book-Author-find('list');
 in books_controller and now books_controller looks like this :

?php
class BooksController extends AppController {
var $name = 'Books';

function index() {
$this-Book-recursive = 1;
$books = $this-Book-find('all');
$this-set('books', $books);
$authors = $this-Book-Author-find('list');
$this-set('authors', $authors);
}
}

then i move on to views/books/index.ctp and wrote ?php print 'pre';
print_r($authors);exit; ? It gave me following error :

*Notice* (8): Undefined property: AppModel::$Author
[*APP\controllers\books_controller.php*, line *9*]

Code | Context

$books  =   array(
array(
Book = array()
),
array(
Book = array()
)
)

$books = $this-Book-find('all');
$this-set('books', $books);$author =
$this-Book-Author-find('list');

BooksController::index() - APP\controllers\books_controller.php, line 9
Object::dispatchMethod() - CORE\cake\libs\object.php, line 116
Dispatcher::_invoke() - CORE\cake\dispatcher.php, line 227
Dispatcher::dispatch() - CORE\cake\dispatcher.php, line 194
[main] - APP\webroot\index.php, line 88


*Fatal error*: Call to a member function find() on a non-object in *
C:\webs\test\relationship\app\controllers\books_controller.php* on line *9

I think models couldn't get associated here. What do u think???



*

On Tue, Oct 12, 2010 at 10:40 PM, Jeremy Burns | Class Outfit 
jeremybu...@classoutfit.com wrote:

 In your add function in the books_controller, you need to populate the
 $authors variable:

 $authors = $this-Book-Author-find('list');

 Then make sure the variable is passed into the view:

 $this-set(compact('authors'));

 or

 $this-set('authors, $authors);

 (they both do the same thing - use the first version if you have more than
 one variable to set and pass)

 Then if you are using the Form helper to build your form, you should have
 this:

 echo $this-Form-input('author_id');

 When Cake draws the author_id input, it will check to see if there is a
 variable that matches (author_id = $authors, book_id = $books, anything_id
 = $anythings - you can follow the pattern). If it finds one (in your case
 $authors) it will assume you want author_id to be a select list, so will use
 the $authors variable as the options.

 You can embellish the input further by adding other options, e.g.:

 echo $this-Form-input(
 'author_id',
 array(
 'empty' = true
 )
 );

 The empty option means that no value is already selected in the list; by
 default the first option value (your first author) is pre-populated. You can
 also do:

 'empty' = 'Please choose an author'


 Jeremy Burns
 *Class Outfit*
 *
 *
 jeremybu...@classoutfit.com jeremybu...@mac.com
 http://www.classoutfit.com

 On 12 Oct 2010, at 17:59, Ashwani Kumar wrote:

 Hi jeremy! Thanx for the reply. I deleted all files from cakephp except
 authors_controller and books_controller and then i added var $scaffold in
 both the files and also deleted all functions from both files. Then i added
 some authors. Now at the time of adding books, Author field should be a Drop
 down menu. But its not happening in my case. Author field is just a Text
 field here in my case. If you could explain why this is happening? is this
 an issue with my cakephp version of what?? By the way i'm using CakePHP 1.3

 On Tue, Oct 12, 2010 at 8:13 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:

 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 

Re: Please Help : I Can't get data of both associated models that are being $hasMany and $belongsTo model

2010-10-12 Thread Jeremy Burns | Class Outfit
Remove $this-Book-recursive = 1;  That line is effectively stopping any 
searches pat the Book model.

Did you follow my other tip about Containable?

A tip; instead of doing pre pre etc... just do debug($authors);

Jeremy Burns
Class Outfit

jeremybu...@classoutfit.com
(t) +44 (0) 208 123 3822
(m) +44 (0) 7973 481949
Skype: jeremy_burns
http://www.classoutfit.com

On 12 Oct 2010, at 18:28, Ashwani Kumar wrote:

 Hi Jeremy! I added 
 $authors = $this-Book-Author-find('list');
  in books_controller and now books_controller looks like this : 
 
 ?php
 class BooksController extends AppController {
 var $name = 'Books';
 
 function index() {
 $this-Book-recursive = 1;
 $books = $this-Book-find('all');
 $this-set('books', $books);
 $authors = $this-Book-Author-find('list');
 $this-set('authors', $authors);
 }
 }
 
 then i move on to views/books/index.ctp and wrote ?php print 'pre'; 
 print_r($authors);exit; ? It gave me following error : 
 
 Notice (8): Undefined property: AppModel::$Author 
 [APP\controllers\books_controller.php, line 9]
 
 Fatal error: Call to a member function find() on a non-object in 
 C:\webs\test\relationship\app\controllers\books_controller.php on line 9
 
 I think models couldn't get associated here. What do u think???
 
 
 
 
 On Tue, Oct 12, 2010 at 10:40 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:
 In your add function in the books_controller, you need to populate the 
 $authors variable:
 
 $authors = $this-Book-Author-find('list');
 
 Then make sure the variable is passed into the view:
 
 $this-set(compact('authors'));
 
 or
 
 $this-set('authors, $authors);
 
 (they both do the same thing - use the first version if you have more than 
 one variable to set and pass)
 
 Then if you are using the Form helper to build your form, you should have 
 this:
 
 echo $this-Form-input('author_id');
 
 When Cake draws the author_id input, it will check to see if there is a 
 variable that matches (author_id = $authors, book_id = $books, anything_id 
 = $anythings - you can follow the pattern). If it finds one (in your case 
 $authors) it will assume you want author_id to be a select list, so will use 
 the $authors variable as the options.
 
 You can embellish the input further by adding other options, e.g.:
 
 echo $this-Form-input(
   'author_id',
   array(
   'empty' = true 
   )
 );
 
 The empty option means that no value is already selected in the list; by 
 default the first option value (your first author) is pre-populated. You can 
 also do:
 
 'empty' = 'Please choose an author'
 
 
 Jeremy Burns
 Class Outfit
 
 jeremybu...@classoutfit.com
 http://www.classoutfit.com
 
 On 12 Oct 2010, at 17:59, Ashwani Kumar wrote:
 
 Hi jeremy! Thanx for the reply. I deleted all files from cakephp except 
 authors_controller and books_controller and then i added var $scaffold in 
 both the files and also deleted all functions from both files. Then i added 
 some authors. Now at the time of adding books, Author field should be a Drop 
 down menu. But its not happening in my case. Author field is just a Text 
 field here in my case. If you could explain why this is happening? is this 
 an issue with my cakephp version of what?? By the way i'm using CakePHP 1.3 
 
 On Tue, Oct 12, 2010 at 8:13 PM, Jeremy Burns | Class Outfit 
 jeremybu...@classoutfit.com wrote:
 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(