I googled this a lot but nothing seems to be similar to what i am
looking for..

What i am trying to do is to link one record "User" with other
multiple records "Cards" at once using a multiple HTML <select> list.

Tables are:
users (id, name, username)
cards (id, user_id)

Models:

    class User extends AppModel {
       public $name = 'User';
       public $hasMany = array('Card');}

    class Card extends AppModel {
       public $name = 'Card';
       public $belongsTo = array('User');}

User edit.ctp view

    echo $this->Form->input('id');
    echo $this->Form->input('Card', array('multiple'=>true));

How my controller gonna look like? currently it looks like this but
saves nothing but the User record "without the related cards"

        if (!empty($this->data)) {
    foreach($this->data['User']['Card'] as $key => $item){
                                $this->data['User']['Card'][$key] =
array('id'=>$item, 'user_id'=>$this->data['User']['id']);
                            }
                        if ($this->User->saveAll($this->data)) {
                                 //$this->User->Card->saveAll($this-
>data);
                                $this->Session->setFlash(__('The user has been 
saved', true));
                                $this->redirect(array('action' => 'index'));
                        } else {
                                $this->Session->setFlash(__('The user could not 
be saved.
Please, try again.', true));
                        }
                }
        if (empty($this->data)) {
                        $this->data = $this->User->read(null, $id);
                             $this->set('cards', $this->User->Card-
>find('list'));
                }

$this->data contains:

            Array
        (
            [User] => Array
                (
                    [id] => 1
                    [name] => Superman
                    [status] => 1
                    [Card] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 11402130001
                                    [user_id] => 1
                                )

                            [1] => Array
                                (
                                    [id] => 11402130002
                                    [user_id] => 1
                                )

                            [2] => Array
                                (
                                    [id] => 11402130003
                                    [user_id] => 1
                                )

                            [3] => Array
                                (
                                    [id] => 11402130004
                                    [user_id] => 1
                                )

                        )

                )
    )

Thanks

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to