Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Linking Models Together
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
newt_e  
View profile  
 More options Jul 29 2012, 4:24 pm
From: newt_e <new...@blueyonder.co.uk>
Date: Sun, 29 Jul 2012 13:24:36 -0700 (PDT)
Local: Sun, Jul 29 2012 4:24 pm
Subject: Linking Models Together

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tilen Majerle  
View profile  
 More options Jul 29 2012, 4:58 pm
From: Tilen Majerle <tilen.maje...@gmail.com>
Date: Sun, 29 Jul 2012 22:58:00 +0200
Subject: Re: Linking Models Together

it always search for primary key, set $primaryKey variable in model to your
column
--
Lep pozdrav, Tilen Majerle
http://majerle.eu

2012/7/29 newt_e <new...@blueyonder.co.uk>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
newt_e  
View profile  
 More options Jul 31 2012, 5:53 pm
From: newt_e <new...@blueyonder.co.uk>
Date: Tue, 31 Jul 2012 14:53:25 -0700 (PDT)
Local: Tues, Jul 31 2012 5:53 pm
Subject: Re: Linking Models Together

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Baird  
View profile  
 More options Jul 31 2012, 7:10 pm
From: Daniel Baird <danielba...@gmail.com>
Date: Wed, 1 Aug 2012 09:10:17 +1000
Local: Tues, Jul 31 2012 7:10 pm
Subject: Re: Linking Models Together

http://book.cakephp.org/2.0/en/models/associations-linking-models-tog...

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
newt_e  
View profile  
 More options Aug 1 2012, 4:14 pm
From: newt_e <new...@blueyonder.co.uk>
Date: Wed, 1 Aug 2012 13:14:07 -0700 (PDT)
Local: Wed, Aug 1 2012 4:14 pm
Subject: 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lowpass  
View profile  
 More options Aug 2 2012, 9:21 pm
From: lowpass <zijn.digi...@gmail.com>
Date: Thu, 2 Aug 2012 21:21:07 -0400
Local: Thurs, Aug 2 2012 9:21 pm
Subject: Re: Linking Models Together
That also makes it rather difficult to list authors alphabetically.
Better to do it the right way the first time than to have to come up
with hacks later.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »