Wednesday, August 1, 2012

Re: Linking Models Together

Gah - I'm a numpty. Kept putting the foreignKey as the column on the table I'm joining to.

As for books/authors - I took the simple approach and gave each book one author. If a book is written by John Smith and Bob Jones, then there is a row in the author table "John Smith & Bob Jones". Makes the design easier, but it does mean if I want to find all the books by Bob Jones then I need to search for "%Bob Jones%'. But I may change that design anyway, but at least I can now do all the other foreignKey look ups.

Thanks,

N.

On Wednesday, August 1, 2012 12:10:17 AM UTC+1, Daniel Baird wrote:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#belongsto 
...is where belongsTo is documented, you should look over that.

But -- even though I hate to be that jerk who points out problems other than the one you asked about -- before you get too far, the data model you describe only lets a book have a single author.  If you're storing actual real-world books in there, you probably want to support books having multiple authors, which means a HABTM "hasAndBelongsToMany" association.  For that you need a third table authors_books, etc as described in the cookbook.

Good luck

;Daniel

--
Daniel Baird
I've tried going to the XHTML <bar /> a few times, but it's always closed.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
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

No comments: