How do you specify a Dealer or Location in the form? Normally, Cake would expect $data to be, eg:
$data['Artwork']['dealer_id'] => x However, if the form simply has a text field to enter that information, Cake won't know anything about the existing record. I think the thing to do in this case is to provide select lists for Dealer & Location (using Artwork.dealer_id & Artwork.location_id as the keys) and text fields that can be filled in if a new Dealer or Location is required (using Dealer.name & Location.name, or whatever the field is in those tables). If the select lists are used, $data will look as described above and the Artwork will be associated with an existing record. However, if either are empty, your controller would expect to have, eg: $data['Dealer']['name'] => 'foo' If that's the case, simply create a new Dealer, save it, collect the primary key, do unset($this->data['Dealer']), add the PK to $this->data['Artwork']['dealer_id'] and save away. The bottom line is that you always want to be working with IDs (ie. primary/foreign keys) when working with associated data. Otherwise, it's tedious and difficult to match records. An example would be a form field for country. If a text field is provided, it becomes difficult to then do things like select all records for a particular country because there may be a multitude of spellings. By providing a select list, the user will always provide the same ID for any country. On Wed, Aug 26, 2009 at 7:54 AM, bg<[email protected]> wrote: > > dear cakephp users, > > i am in the process of extending a simple cakephp app which now > requires me to use multiple tables and data associations. now i'm > stuck at a problem and i looking for help! > > basically i have one form from which i want to save/update to several > models. when the user adds or edits an artwork through the form, a new > entry should be created or an entry should be updated in the artwork > model. each artwork has a dealer, an artist and a location, which in > most cases already exist in respective table. so what i want is, when > i save the form in the artwork_controller, the app should check > whether the dealer, artist or location entered in the form already > exist and save this association using the respective_id to the artwork > model, or if not, create new one. right now, when i use saveAll, it > always creates new entries in the associated dealer, artist or > location tables. > > these are my models: > > class Artwork extends AppModel { > var $name = 'Artwork'; > var $belongsTo = array( > 'Artist', 'Dealer', 'Location' > ); > } > > class Artist extends AppModel { > var $name = 'Artist'; > var $hasMany = array( > 'Artwork' => array( > 'className' => 'Artwork', > 'dependent'=> false > ) > ); > } > > class Dealer extends AppModel { > var $name = 'Dealer'; > var $hasMany = array( > 'Artwork' => array( > 'className' => 'Artwork', > 'dependent'=> false > ) > ); > } > > class Location extends AppModel { > var $name = 'Location'; > var $hasMany = array( > 'Artwork' => array( > 'className' => 'Artwork', > 'dependent'=> false > ) > ); > } > > i would highly appreciate if someone could help me understand how i > have to extend the artworks_controller add() and edit($id) > functions... > > thank you for your attention! > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
