Hello guys,

I'm new to cakePHP and I'm trying to try a HABTM insert into database
but it's not working, this is what I got:

A table for users, a table for lyfestyles and a lifestyles_users
table, this are my models:

class User extends AppModel
{
        var $name = 'User';
        var $hasAndBelongsToMany = array('Lifestyle' =>
                           array('className'    => 'Lifestyle',
                                 'joinTable'    => 'lifestyles_users',
                                 'foreignKey'   => 'user_id',
                                 'associationForeignKey'=>
'lifestyle_id',
                                 'conditions'   => '',
                                 'order'        => '',
                                 'limit'        => '',
                                 'unique'       => true,
                                 'finderQuery'  => '',
                                 'deleteQuery'  => '',
                           )
                );
}


class Lifestyle extends AppModel
{
        var $name = 'Lifestyle';
        var $hasAndBelongsToMany = array('User' =>
                           array('className'    => 'User',
                                 'joinTable'    => 'lifestyles_users',
                                 'foreignKey'   => 'lifestyle_id',
                                 'associationForeignKey'=> 'user_id',
                                 'conditions'   => '',
                                 'order'        => '',
                                 'limit'        => '',
                                 'unique'       => '',
                                 'finderQuery'  => '',
                                 'deleteQuery'  => '',
                           )
                );
}


And my join table has this structure:

CREATE TABLE `lifestyles_users` (
  `lifestyle_id` int(10) unsigned NOT NULL default '0',
  `user_id` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`lifestyle_id`,`user_id`)
) ENGINE=MyISAM;


The first thing I do is to insert a new user by registering it, then
in the lifestyle action of the users_controller.php I can set its
lifestyle, this is my code:

                function lifestyle()
                {
                        $this->pageTitle = 'Mi Lifestyle';
                        $this->User->id = $this->Session->read('user_id');
                        if(empty ($this->data))
                        {
                                $this->data = $this->User->read();
                                $this->set('User', $this->data);
                                $this->set('lifestyles', $this->User->Lifestyle-
>generateList(null, null, null, '{n}.Lifestyle.id',
'{n}.Lifestyle.lifestyle'));
                                $this->set('deportes', $this->User->Lifestyle-
>generateList('WHERE Lifestyle.tipo="deportes"', null, null,
'{n}.Lifestyle.id', '{n}.Lifestyle.lifestyle'));
                                $this->set('entretenimiento', 
$this->User->Lifestyle-
>generateList('WHERE Lifestyle.tipo="entretenimiento"', null, null,
'{n}.Lifestyle.id', '{n}.Lifestyle.lifestyle'));
                                $this->set('cultura', $this->User->Lifestyle-
>generateList('WHERE Lifestyle.tipo="cultura"', null, null,
'{n}.Lifestyle.id', '{n}.Lifestyle.lifestyle'));
                                $this->set('viajes', $this->User->Lifestyle-
>generateList('WHERE Lifestyle.tipo="viajes"', null, null,
'{n}.Lifestyle.id', '{n}.Lifestyle.lifestyle'));
                        }
                        else
                        {
                                if ($this->User->save($this->data))
                                {
                                        $this->flash('Tu lifestyle ha sigo 
guardado','/users/view/' .
$this->Session->read('user_id'));
                                }
                                else
                                {
                                        $this->flash('Tu lifestyle NO pudo ser 
guardado','/users/
view/' . $this->Session->read('user_id'));
                                }
                        }
                }


And this is my view where I send lifestyles in the form manual says to
(data[ModelName][ModelName]), in this case data[Lifestyle][Lifestyle]:


<?php echo $html->formTag('/users/lifestyle') ?>

<table>
        <tr>
                <td>
                        <h1>Deportes:</h1>
                        <label>Elige uno o m&aacute;s de la lista</label><br />
                        <?php echo $html->selectTag('Lifestyle/Lifestyle', 
$deportes, null,
array('multiple' => 'multiple')) ?>
                </td>
                <td>
                        <h1>Entretenimiento:</h1>
                        <label>Elige uno o m&aacute;s de la lista</label><br />
                        <?php echo $html->selectTag('Lifestyle/Lifestyle',
$entretenimiento, null, array('multiple' => 'multiple')) ?>
                </td>
        </tr>
        <tr>
                <td>
                        <h1>Cultura</h1>
                        <label>Elige uno o m&aacute;s de la lista</label><br />
                        <?php echo $html->selectTag('Lifestyle/Lifestyle', 
$cultura, null,
array('multiple' => 'multiple')) ?>
                </td>
                <td>
                        <h1>Viajes</h1>
                        <label>Elige uno o m&aacute;s de la lista</label><br />
                        <?php echo $html->selectTag('Lifestyle/Lifestyle', 
$viajes, null,
array('multiple' => 'multiple')) ?>
                </td>
        </tr>
</table>

<?php echo $html->submitTag('Guardar') ?>
</form>

What am I doing wrong? I hope you can help me guys, thanks in advance!


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to