On Sat, Jan 3, 2009 at 9:41 PM, mike <[email protected]> wrote:
>
> ...
>
> 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); ?>


My bad. I forgot to mention the weird naming convention needed for HABTM:

<? echo $form->input('Ethnicity.Ethnicity', array( 'type' => 'select',
'multiple' => 'checkbox' ),$ethnicities); ?>

No, I don't understand why.


> 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 only thing I see odd about that is it should be

$this->User->Ethnicity->find(...

It's the model, not the controller, that "has" the other model. Unless
you've loaded the Ethnicity model in the controller (which likely
isn't necessary).


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


It should look like this when it reaches your controller:

[data] => Array
   (
           [User] => Array
                   (
                           [age] => 50
                           [about_me] => sadfadfs
                   ),
           [Ethnicity] => Array
                   (
                           [0] => 1
                           [1] => 3
                   )
   )

Note there's only 1 'Ethnicity' key, in spite of the $form param. Go figure.


> 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!");
>                                                }
>                        }
>        }


Once the data is coming in properly, you should only need:

function add()
{
        if (!empty($this->data))
        {
                if ($this->User->save($this->data))
                {
                        $this->Session->setFlash("User Saved!");
                }
        }

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

yahoo, eh ;-)

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

Reply via email to