Re: saving hasAndBelongsToMany without form?
> Aha! I think I actually had it right before, but Cake appears to care > about the order of the top-level array. So if it goes ('Post', 'Tag') > it works but if it goes ('Tag', 'Post) it doesn't. > > It would be neat if a future version of Cake could take this a step > further and create associated records on the fly. So if instead of > supplying an ID you supplied an array of fields (like you do for a > normal save operation), it would create the relevant tag record. Right > now saving with existing tags is easy, but I still need to create new > ones manually. > here's a snippet from one of my controllers, it makes it possible to have a single text input, into which people enter their tags as a comma separated list. It checks to see if a tag already exists before creating the association. $i = 0; foreach ($tags as $tag) { if ($tag != " " && $tag != "") { $tag = trim($tag); if (!$result = $this->Controller->Tag->findByName ($tag)) { $data['Tag'] = array(); $data['Tag']['id'] = null; $data['Tag']['name'] = $tag; $data['Tag']['handle'] = $this->Sanitize->paranoid($tag); $data['Tag']['description'] = null; $this->Controller->Tag->save($data); $result = $this->Controller->Tag->read(); } } $this->params['data']['Tag']['Tag'][$i] = $result['Tag']['id']; ++$i; } $this->Controller->save($this->params['data']); hth jb -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: saving hasAndBelongsToMany without form?
Aha! I think I actually had it right before, but Cake appears to care about the order of the top-level array. So if it goes ('Post', 'Tag') it works but if it goes ('Tag', 'Post) it doesn't. It would be neat if a future version of Cake could take this a step further and create associated records on the fly. So if instead of supplying an ID you supplied an array of fields (like you do for a normal save operation), it would create the relevant tag record. Right now saving with existing tags is easy, but I still need to create new ones manually. X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.11.53.63 with SMTP id b63mr86705cwa; Sat, 20 May 2006 09:59:02 -0700 (PDT) X-Google-Token: esSaVAwAAAD6v3WBKeD10FWVKfBPzicH Received: from 24.61.43.8 by i40g2000cwc.googlegroups.com with HTTP; Sat, 20 May 2006 16:59:02 + (UTC) From: "Dave" <[EMAIL PROTECTED]> To: "Cake PHP" Subject: Re: saving hasAndBelongsToMany without form? Date: Sat, 20 May 2006 09:59:02 -0700 Message-ID: <[EMAIL PROTECTED]> In-Reply-To: <[EMAIL PROTECTED]> References: <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8) Gecko/2005 Firefox/1.5,gzip(gfe),gzip(gfe) Mime-Version: 1.0 Content-Type: text/plain Aha! I think I actually had it right before, but Cake appears to care about the order of the top-level array. So if it goes ('Post', 'Tag') it works but if it goes ('Tag', 'Post) it doesn't. It would be neat if a future version of Cake could take this a step further and create associated records on the fly. So if instead of supplying an ID you supplied an array of fields (like you do for a normal save operation), it would create the relevant tag record. Right now saving with existing tags is easy, but I still need to create new ones manually. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: saving hasAndBelongsToMany without form?
> To put it more simply: Can someone provide a code snippet with a call > to Post->save() whose data is properly structured to save both the Post > and one or more Tags with it? for each tag you need: $this->params['data']['Tag']['Tag'][n] = $tag_id; then when you call: $this->Post->save($this->params['data']); cake will do the rest. hth jb -- jon bennett t: +44 (0) 1225 341 039 w: http://www.jben.net/ iChat (AIM): jbendotnet Skype: jon-bennett --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: saving hasAndBelongsToMany without form?
To put it more simply: Can someone provide a code snippet with a call to Post->save() whose data is properly structured to save both the Post and one or more Tags with 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 -~--~~~~--~~--~--~---
saving hasAndBelongsToMany without form?
I'm building an app that does most of its work via AJAX, so I don't think the form-based methods for working with associations in the docs will work, and I can't figure out how to update and create hasAndBelongsToMany relationships from a controller. What I have is basically a posts table, a tags, table, and a posts_tags table as expected. The HABTM relationship works perfectly for retrieving data: When I retrieve posts I get the tags along with them. But saving is another matter. I've tried passing various permutations of the following array to Post->save(): Array ( [Post] => Array ( [name] => postname [id] => 138 ) [Tag] => Array ( [0] => Array ( [name] => test tag ) ) ) ...which I would expect to save a relationship between post #138 and the tag named "test tag", creating the tag if necessary. Is that possible? If so, what am I doing wrong? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---