zonium: yes, you're absolutely right. and I haven't tested it...
Remember to put two associations in the address model as well, address
hasOne (or hasMany, depending on what you want) customer
(default_address_id) and address belongsTo customer...

On 29 Nov, 10:27, zonium <[EMAIL PROTECTED]> wrote:
> I think your setup is not impractical, in fact it's a very common type
> of relationships, for example:
> User hasMany Address, and there is one Address that is used as
> default
> Theme hasMany Layout, and there is one Layout that is set as current
> etc.
> Using array as Christopher suggested is a solution I often use, but
> specifying an additional association as J.B. described seems to be a
> better solution.
> However, the association should be 'belongsTo' instead of
> 'hasOne' (not sure if J.B. has tested)
> The reason is default_address_id' is foreign_key in customers table,
> and it corresponds to address_id on addresses table.
>
> in the Customer model, try
> $belongsTo = array(
>                         'Address' => array('className' => 'Address',
>                                                                 'foreignKey' 
> => 'default_address_id',
>                                              ),
>                         );
>
> in the controller, try:
>
> $this->Customer->findAll(array("id"=>'123456'))
>
> and you will see the default address followed by an array of addresses
> (including the default address as well !)
>
> Zonium
>
> On Nov 28, 3:55 pm, "J.B." <[EMAIL PROTECTED]> wrote:
>
> > This can be done by adding another association, hasOne, on the model.
> > The customers model will then have one hasOne and one hasMany
> > association to the same model (addresses), just remember to name the
> > associations different names and to use default_address_id as the
> > foreign_key for the hasOne association.
> > Code:
> > var $hasMany = array('Address' => array('name' => 'Address'));
> > var $hasOne = array('DefaultAddress' => array('name' => 'Address',
> > {foreign_key stuff here...}));
>
> > I think it's something like that :P
>
> > On 29 Nov, 00:02, FrenchEscapes <[EMAIL PROTECTED]> wrote:
>
> > > I have a customers table and an addresses table, customers hasMany
> > > addresses, that bit's easy.
>
> > > But I want each customer to have a default address in the addresses
> > > table. I have a field 'default_address_id' in the customer table. I
> > > just can't get my head around how to use associations to link them.
>
> > > Thank you in advance.
>
> > > Duncan

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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