Saving hasAndBelongsToMany with selectTag
Hi, Oddly enough Cake doesn't seem to understand that it should make an entry in the join table for HABTM related models when the foreign key isn't supplied from a multi select field... Let me try and explain that :) Say you are creating a new Item which belongs to one or many Persons, you will need a select tag with the name Person/Person. However it seems this needs to be a select tag with multiple selection enabled. This is so that the form generates the following: Array ( [Item] = Array ( [field] = Value ) [Person] = Array ( [Person] = Array ( [0] = 1 ) ) ) In other words you can't use a normal select tag, as this will generate: [Person] = Array ( [Person] = 1 ) As the value of person isn't an array here, Cake won't pick it up and won't make an entry in the join table. I haven't found a solution for this yet, I'm assuming that I'm either missing something or that I'm going to have to do some ugly hack in the controller's add/edit functions to turn the value into an array. Simon --~--~-~--~~~---~--~~ 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: Saving hasAndBelongsToMany with selectTag
The way I've worked around it for the moment is to add the following to the add function in the controller: if( !is_array($this-data['Person']['Person']) ) { $this-data['Person']['Person'] = aa(0, $this-data['Person'] ['Person']); } It's not pretty, I know. On May 23, 3:18 pm, simon [EMAIL PROTECTED] wrote: Hi, Oddly enough Cake doesn't seem to understand that it should make an entry in the join table for HABTM related models when the foreign key isn't supplied from a multi select field... Let me try and explain that :) Say you are creating a new Item which belongs to one or many Persons, you will need a select tag with the name Person/Person. However it seems this needs to be a select tag with multiple selection enabled. This is so that the form generates the following: Array ( [Item] = Array ( [field] = Value ) [Person] = Array ( [Person] = Array ( [0] = 1 ) ) ) In other words you can't use a normal select tag, as this will generate: [Person] = Array ( [Person] = 1 ) As the value of person isn't an array here, Cake won't pick it up and won't make an entry in the join table. I haven't found a solution for this yet, I'm assuming that I'm either missing something or that I'm going to have to do some ugly hack in the controller's add/edit functions to turn the value into an array. Simon --~--~-~--~~~---~--~~ 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: Saving hasAndBelongsToMany with selectTag
The way I've worked around it for the moment is to add the following to the add function in the controller: if( !is_array($this-data['Person']['Person']) ) { $this-data['Person']['Person'] = aa(0, $this-data['Person'] ['Person']); } It's not pretty, I know. On May 23, 3:18 pm, simon [EMAIL PROTECTED] wrote: Hi, Oddly enough Cake doesn't seem to understand that it should make an entry in the join table for HABTM related models when the foreign key isn't supplied from a multi select field... Let me try and explain that :) Say you are creating a new Item which belongs to one or many Persons, you will need a select tag with the name Person/Person. However it seems this needs to be a select tag with multiple selection enabled. This is so that the form generates the following: Array ( [Item] = Array ( [field] = Value ) [Person] = Array ( [Person] = Array ( [0] = 1 ) ) ) In other words you can't use a normal select tag, as this will generate: [Person] = Array ( [Person] = 1 ) As the value of person isn't an array here, Cake won't pick it up and won't make an entry in the join table. I haven't found a solution for this yet, I'm assuming that I'm either missing something or that I'm going to have to do some ugly hack in the controller's add/edit functions to turn the value into an array. Simon --~--~-~--~~~---~--~~ 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: Saving hasAndBelongsToMany with selectTag
On 5/23/07, simon [EMAIL PROTECTED] wrote: Hi, Oddly enough Cake doesn't seem to understand that it should make an entry in the join table for HABTM related models when the foreign key isn't supplied from a multi select field... Let me try and explain that :) Well, considering other people seem to be able to use HABTM-related models, I'd say that perhaps something is wrong in your configuration. :) Past your models into the CakeBin (http://bin.cakephp.org) so we can take a look at it. I assume you've also read up on the section in the manual dealing with associations and you understand what all the configuration variables for associations mean? -- Chris Hartjes My motto for 2007: Just build it, damnit! @TheBallpark - http://www.littlehart.net/attheballpark @TheKeyboard - http://www.littlehart.net/atthekeyboard --~--~-~--~~~---~--~~ 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: Saving hasAndBelongsToMany with selectTag
Well, considering other people seem to be able to use HABTM-related models, I'd say that perhaps something is wrong in your configuration. :) The weird thing is that everything works perfectly normally as long as I use a multi-select field, i.e. a select tag that has the multiple attribute. The whole chain of saving and reading works and entries are made in the join table. All the examples I've found in the manual and online also all use multi-selects. I should point out that this is only if you use the HTML helper to generate the field - otherwise all you should need to do is add []: select name=data[Person][Person][].../select --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---