Tuesday, June 30, 2009

Re: Simple relationship problem

Carlos is right, but he was a little implicit in his answer.

The short answer is that the Agency belongsTo Seller means that each
agency belongs to only one seller. It wants a seller_id field in the
agency table (which is how the relationship is linked).

From what I understand, you want the Seller to belongTo an agency (so
sellers table will need an agency_id in it), and you want the agency
model to contain a hasMany sellers relationship (necessitating an
agency_id foreign key in the sellers table). So try the hasMany.

BTW, you only need to define the relationships both ways if you need
to access things in the opposite direction. So, you could do a
sellers belongsTo agency, but you don't necessarily need the opposite
relationship UNLESS you want to retrieve associated sellers during a
query on the agency model. But it's a good practice to do so, because
the database cost to crosslink the two is only one more INT field.

If you need the capability for a seller to belong to many agencies,
you'll need the HABTM. Which will require a join table
(agencys_sellers). And it will likely mean you'll need the
Containable behavior in some of your stuff too.

On Jun 30, 7:36 am, Carlos Gonzalez Lavin <carloslavi...@gmail.com>
wrote:
> How about:
> *Seller Model:
> *
> *belongsTo agency*
> *
> *
> *Agency Model:
> **hasMany seller*
> *
> *
> Because Agency doesn't really belong to the seller... an agency has many
> sellers (if I understand you correctly) and a seller belongs to an agency
>
> 2009/6/30 Bryan Paddock <bryanpadd...@gmail.com>
>
>
>
> > Hey JD,
>
> > Ah... I'm confused as to why I would need a HABTM and a join table for
> > a fairly simple relationship.
>
> > example of the structure:
>
> > Seller.id
> > Seller.name
> > Seller.agency_id
> > etc
>
> > and
>
> > Agency.id
> > Agency.name
> > etc
>
> > Each seller can only belong to one agency.
> > Each agency can have any number of sellers.
>
> > I've defined $hasOne and $belongsTo but cake complains that there is
> > no agency.seller_id field which is correct because that would imply
> > that each agency can only have one seller.
>
> > Hmmmmm.....
>
> > On Mon, Jun 29, 2009 at 8:25 PM, JD Daniels<j...@internetguys.ca> wrote:
> > > You need to look at hasAndBelongsToMany
>
> > >http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM
>
> > > Bryan Paddock wrote:
>
> > > Argh.. I hate gmail shortcut keys.
>
> > > Anyways continuing mail:
>
> > > Heya all,
>
> > > Theres something I'm not doing right here...
>
> > > I have a User model which has one Seller model linked to it which in
> > > turn can have an Agency model linked to it.
>
> > > User Model:
> > > hasOne seller.
>
> > > Seller Model:
> > >  belongsTo user
>
> > > Agency Model:
> > > belongsTo seller
>
> > > Now when I call $this->User->find('all') it complains that there is no
> > > foreign key in the agency table. That would be impossible because then
> > > that would mean only one agency can belong to one seller.
>
> > > How do I set it up so that any number of agencies can belong to any
> > > number of sellers? I'm missing something really stupid here....
>
> > > Thanks!
>
> > > Bryan
>
> > > On Mon, Jun 29, 2009 at 7:06 PM, Bryan Paddock<bryanpadd...@gmail.com>
> > > wrote:
>
> > > Heya all,
>
> > > Theres something I'm not doing right here...
>
> > > I have a User model which has one Seller model linked to it which in
> > > turn can have an Agency model linked to it.
>
> > > User Model:
> > > hasOne seller.
> > > Seller Model: belongsTo user
> > > Agency Model: belongsTo seller
--~--~---------~--~----~------------~-------~--~----~
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: