You should save each separately. Set up your association as HABTM.
Event model:
public $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',
'joinTable' => 'events_tags',
'foreignKey' => 'event_id',
'associationForeignKey'=> 'tag_id'
)
);
Tag model:
public $hasAndBelongsToMany = array(
'Post' => array(
'className' => 'Event',
'joinTable' => 'events_tags',
'foreignKey' => 'tag_id',
'associationForeignKey'=> 'event_id'
)
);
In your controller action:
$this->set('tags', $this->Event->Tag->find('list'));
In your view:
echo $form->label('Tag', 'Tag(s)', array('class' => 'Required'));
echo $form->error('Tag', 'Please choose at least one tag.');
echo $form->select('Tag', $tags, null, array('class' => 'Required',
'multiple' => 'checkbox'));
back in the controller action:
/* ensure that at least one Tag was chosen
*/
$this->Event->Tag->set($this->data);
if (!$this->Event->Tag->validates())
{
$this->Event->Tag->invalidate('Tag');
}
else if ($this->Event->save($this->data))
{
On Thu, Aug 6, 2009 at 9:17 AM,
WhyNotSmile<[email protected]> wrote:
>
> I'm using CakePHP 1.2 and am trying to create a form which allows
> people to submit various bits of information (about their upcoming
> event). I want them to be able to add tags to the event, from a pre-
> defined set.
>
> So, what I was going to do was to have a list of all possible tags
> (there won't be that many - max 10 or so), and have them as
> checkboxes, so people can select as many or as few as they want. The
> plan is then to store them in some sort of array, so each tag would
> have an id, and they would get stored in the database as a list.
>
> For example, if someone selects tags 1, 5 and 7, then the database
> field will be '1,5,7'.
>
> Two questions:
> 1. Does this sound like a sensible way of doing this? Or can anyone
> suggest a better way? I'm not that up to speed on cake, but I know
> the basics.
>
> 2. Umm... how do I actually do this? I can't figure out how to have
> checkboxes where people can select as many or as few as they want.
> Any tips would be welcome!
>
> Thanks!
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---