Saturday, January 3, 2009

saving to habtm models from checkbox groups.

I posted about this earlier but I'm still having trouble. I think I
have my models set up properly now:

class User extends AppModel
{
var $name = 'User';
var $hasAndBelongsToMany = array ( 'Ethnicity' => array(
'className' => 'Ethnicity',
'joinTable' =>
'ethnicities_users',
'foreignKey' => 'user_id',
'associationForeignKey' =>
'ethnicity_id'
)
);
}


class Ethnicity extends AppModel
{
var $name = 'Ethnicity';
var $hasAndBelongsToMany = array ( 'User' => array(
'className' => 'User',
'joinTable' =>
'ethnicities_users',
'foreignKey' => 'ethnicity_id',
'associationForeignKey' => 'user_id'
)
);
}

I've been trying different stuff in the controller, this is what I
have now:
function add() {
$this->User->create();
if(!empty($this->data)) {
if($this->User->save($this->data)) {
$this->data['User']['Ethnicity']['user_id'] = $this->User-
>getLastInsertId();
$this->User->Ethnicity->save($this->data);
$this->Session->setFlash("User Saved!");
}
}
}

here is what I have in the view, which I don't think is correct:

<? echo $form->create('User', array('action'=>'add')); ?>
<? echo $form->input('User.Ethnicity', array( 'type' => 'select',
'multiple' => 'checkbox' ),$ethnicities); ?>
<?php echo $form->end('Submit'); ?><br/>

form the research I've done, I feel like the input fieldname should be
'User.Ethnicity. .id' or something like that. However, anytime I add
the 'id' into the fieldname, the checkboxes don't get created in the
html anymore. here's how $ethnicities is populated in the controller,
if that matters:

$this->set('ethnicities',$this->Ethnicity->find('list', array('fields'
=> array('id','ethnicityName'))));

the result right now is this:

[data] => Array
(
[User] => Array
(
[Ethnicity] => Array
(
[0] => 1
[1] => 3
[user_id] => 34
)

[age] => 50
[about_me] => sadfadfs
)

)

but my understanding is I need something like this:
[data] => Array
(
[User] => Array
(
[Ethnicity] => Array
(
[ethnicity_id] => 1
[user_id] => 34,

[ethnicity_id] => 2
[user_id] => 34
)

[age] => 50
[about_me] => sadfadfs
)

)


here is a link to my other thread for reference:

http://groups.google.com/group/cake-php/browse_thread/thread/2c74ee5184fe8d90/62788d3299b36a31?lnk=gst&q=checkbox+groups#62788d3299b36a31

any help apprciated. thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: