personal preference. The only real advantage of using InnoDB with Cake
is its transaction support for saves on multiple models.
You need to explicitly state your associations inside your two models,
Author and Book, i.e.:
author.php:
book.php:
var $belongsTo = array(
'Author' => array('className' => 'Author',
'foreignKey' => 'author_id'
)
);
author.php:
var $hasMany = array(
'Book' => array('className' => 'Book',
'foreignKey' => 'author_id',
'dependent' => false
)
);
Now, due to your recursive level, a find on authors should bring back
all the books they've written!
On Feb 1, 6:13 am, CP <in...@leatherbelts.com> wrote:
> Hi
>
> thanks for the response. I am not sure if I have this figured out but
> I believe its not working for me because my database is a myISAM db.
> From what I read I think cake only suports INNODB joins automatically
> (as to say it can only automatically relate tables based on foreign
> keys which are only possible in an INNODB database). Am I right?
>
> If not and I am still missing something here is the code you
> requested.
>
> views/authors/index.ctp
> <?php foreach($authors as $author): ?><h2><?php echo $author['Author']
> ['name'] ?></h2><hr /><h3>Book(s):</h3><ul><?php foreach($author
> ['Book'] as $book): ?><li><?php echo $book['title'] ?></li><?php
> endforeach; ?></ul><?php endforeach; ?>
>
> /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); }}
>
> ?>
>
> On Jan 31, 10:32 pm, brian <bally.z...@gmail.com> wrote:
>
> > As you're in the authors view, I'll assume that you're trying to
> > retrieve associated Books for Author. If you post the contents of your
> > view action, as well as the $hasMany and $belongsTo arrays for Author
> > and Book respectively, I'm sure someone can help you sort it out.
>
> > Also, if you're using $this->Author->paginate() instead of find(),
> > post your controller's $paginate array.
>
> > On Sat, Jan 31, 2009 at 8:48 PM, CP <in...@leatherbelts.com> wrote:
>
> > > I followed this artcielhttp://www.packtpub.com/article/working-with-simple-associations-usin...
>
> > > But when was unable to get everything working correctly.
> > > I got everything working up to a point but at this point "Retrieving
> > > Related Model Data in One-To-Many Relation" is where I started having
> > > problems.
>
> > > the error I am getting is below:
>
> > > Notice (8): Undefined index: Book [APP/views/authors/index.ctp, line
> > > 1]
>
> > > Anyone have any suggestions? Perhaps I have created the foreign key
> > > incorrectly? I have been trying to figure it out for the past hour and
> > > no luck. I hope someone can help me.
>
> > > Signed
>
> > > new baker- Hide quoted text -
>
> > - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment