Tuesday, July 31, 2012

Re: Linking Models Together


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

On Wed, Aug 1, 2012 at 7:53 AM, newt_e <newt_e@blueyonder.co.uk> wrote:
Hi,

Thanks for the response, but I'm not entirely sure what your suggesting.

An author has many books, and so I can amend the author model to point author.id at book.authorid.
Conversely, many books have the same author, so I want the book model to allow book.authorid to reference author.id. However, as has been pointed out, the model on book wants to use the primary key for book which would be book.id and not book.authorid. There are a number of other fields I need to apply this too as well.

Looking through the cakePHP cookbook, there's section on models and linking models together. At the bottom of the section on hasMany (User hasMany Comment), it says 

"One thing to remember is that you'll need a complimentary Comment belongsTo User association in order to get the date in both directions."

However, it doesn't give any suggestion on what this needs to look like, and that's what I'm trying to set up.

I've not tried this, but do I need to give both keys the same name or something like that?

N.

On Sunday, July 29, 2012 9:58:00 PM UTC+1, MaJerle.Eu wrote:
it always search for primary key, set $primaryKey variable in model to your column
--
Lep pozdrav, Tilen Majerle



2012/7/29 newt_e <snip>

Hi,

I've have two tables:
authors, has a column called id (amongst others)
books, has columns called id and authorid  (amongst others)

Obviously a book has an author, so any value of authorid must exist in the authors table.

When I retrieve an authors row, I also want to return the corresponding authors row (books.authorid = authors.id). 

However, when trying to update my Book model, it always seems to link from the id column - how do I make it work against a different column?

Cheers,
N.

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

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



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