Re: Am I trying to do the impossible (or impractical)?
On Dec 1, 7:53 am, FrenchEscapes [EMAIL PROTECTED] wrote: Well that kind of worked! I have put: var $hasMany = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'customer_id' ), ); var $belongsTo = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'default_address_id', ), ); It's doing it because with the same name the multi dimensional array of hasMany Addresses is being added to/merged with the result of your belongsTo Address. Change the name of one of those aliases (e.g. 'Address' change to 'DefaultAddress'). hth, AD --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
On Dec 1, 1:37 pm, AD7six [EMAIL PROTECTED] wrote: On Dec 1, 7:53 am, FrenchEscapes [EMAIL PROTECTED] wrote: Well that kind of worked! I have put: var $hasMany = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'customer_id' ), ); var $belongsTo = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'default_address_id', ), ); It's doing it because with the same name the multi dimensional array of hasMany Addresses is being added to/merged with the result of your belongsTo Address. Change the name of one of those aliases (e.g. 'Address' change to 'DefaultAddress'). hth, AD Although you'd be better of following Rhee's suggestion imo. AD --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
As I said in my first post: just remember to name the associations different names ... I prefer this method over Rhee's suggestion with a bool for the default address in the database... I think it looks nicer and feel more OO :) On Dec 1, 1:37 pm, AD7six [EMAIL PROTECTED] wrote: On Dec 1, 7:53 am, FrenchEscapes [EMAIL PROTECTED] wrote: Well that kind of worked! I have put: var $hasMany = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'customer_id' ), ); var $belongsTo = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'default_address_id', ), ); It's doing it because with the same name the multi dimensional array of hasMany Addresses is being added to/merged with the result of your belongsTo Address. Change the name of one of those aliases (e.g. 'Address' change to 'DefaultAddress'). hth, AD --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
Sorry to be a stick in the mud, but have changed the name (seems obvious now!!) of the second association to DefaultAddress and that works so it will be staying as is for now. No doubt it will all change before too long and will try the other way. Thanks to all for your help, 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
Well that kind of worked! I have put: var $hasMany = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'customer_id' ), ); var $belongsTo = array( 'Address' = array('className' = 'Address', 'foreignKey' = 'default_address_id', ), ); I am still using scaffolding while I get the structure of the site sorted, when you view a customers record it will give a link to the default address which is perfect, but where it lists the related addresses it shows lists of letters. The letters are the first letter of each field in the default address. confused? Look here: http://i126.photobucket.com/albums/p91/frenchescapes/CakePHP-therapiddevelopmentphpframe.png . The bottom two records are the addresses that are meant to be linked to the customer. Although this is not a problem because eventually I will not be using scaffolding, just a little confused as to why it is doing it! --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
I would put a bool flag in addresses table. Something like `defaultAddress` as bool, where set to true when default and false on the other case. Then you could put WHERE statement into your find function. On Nov 29, 11:50 am, J.B. [EMAIL PROTECTED] wrote: 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 -~--~~~~--~~--~--~---
Am I trying to do the impossible (or impractical)?
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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
Well, if you already have the default_address_id field in the table, you can just do some array magic to pull out the one with that Id or, you can make another call once you have the user array (for a single user). Your best bet is to use the array magic goodness with sprinkles and pluck out what you need. On Nov 28, 3:02 pm, 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
This can be done by adding another association, hasOne... Then the customers model will have both a hasMany and a hasOne association to the same table (addresses). Only remember to specify that default_address_id is the foreign_key for the hasOne association... I think it's something like that :) // J.B. 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 -~--~~~~--~~--~--~---
Re: Am I trying to do the impossible (or impractical)?
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 -~--~~~~--~~--~--~---