A neat trick is to use a "generic" foreign key in Address together
with a variable defining the associated model. That way you can let
anything in your application have an address without adding redundant
fields to the table.

I have used the following in my SQL:
foreign_id int(11),
belongs_to varchar(255)

In person you would write:
var $hasOne = array(
    'Address' => array(
        'className' => 'Address',
        'foreignKey'    => 'foreign_id',
        'conditions'    => 'Address.belongs_to = \'Person\'',
        'dependent' => true
    )
);

In Company you would substitute the condition to match.
Earlier today I saw that AD7six's Upload Behaviour used the same kind
of approach (he called the extra field class, btw) to attach an upload
to any "parent" model. He also had a nifty code to load the parent
model. Check it out if you find the need for some more trickery.

I find this a useful technique when I have a model that is very
generic and central to an application. Many things can have a
serialnumber, address, gps coordinates or whatever you are working on.
Using belongTo just to get the fk in the other model is not always the
best for the application.



On Sep 1, 3:15 pm, bitkidoku <[EMAIL PROTECTED]> wrote:
> Hello,
>
> > Person hasOne Address
> > Company hasOne Address
>
> > Address belongsTo Person
> > Address belongsTo Company
>
> This one seems the best way to do it. Imagine if you ever needed to
> enter multiple addresses for one Person, you only need to change the
> relation from hasOne to hasMany.
>
> What are your concerns about saveAll() ?
>
> Baris
>
> On Sep 1, 3:58 pm, Preloader <[EMAIL PROTECTED]> wrote:
>
> > Hello bakers,
>
> > I have the following relations:
>
> > Person hasOne Address
> > Company hasOne Address
>
> > Address belongsTo Person
> > Address belongsTo Company
>
> > In this case Address should contain person_id and company_id.
>
> > Is this the right way or should I do it the other way round:
>
> > Person belongsTo Address
> > Company belongsTo Address
>
> > Address hasOne Person
> > Address hasOne Company
>
> > Both, Person and Company, must contain address_id now.
>
> > Which one is the more practicable way? Does it matter at all, if I
> > want to use saveAll()?
>
> > Thanks in advance!
>
> > Regards, Christoph
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to