Re: saving hasAndBelongsToMany without form?

2006-05-20 Thread Jon Bennett

> 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?

2006-05-20 Thread Dave

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?

2006-05-20 Thread Jon Bennett

> 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?

2006-05-20 Thread Dave

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
-~--~~~~--~~--~--~---