Question

How should be the format of array to save multiple Users and their Profiles?

There are two models, Users and Profiles. On profiles table I have the foreign key "user_id". I tried using saveAll command for this, but only the Users are saved and profiles table is empty.

Out of for (setting up the array):

$data = array();

Inside of for:

$data[] = array(
    'User' => array(
        'username' => $username,
        'password' => $password
    ),
    'Profile' => array(
        'name' => $name,
    )
);

Save command:

$this->User->saveAll($data)

Model Associations:

User hasMany Profile
Profile belongsTo User

Ps.: saveAll saves 10 users but not their profiles ;/

Was it helpful?

Solution 2

I solved my onw question guys.

There are 2 solutions in this problem.

First:

This code:

User hasMany Profile

must be changed to hasOne:

User hasOne Profile

Second:

If you have a hasMany relation, you need to create an diferent array, using numeric keys on Profile even if you will add only one Profile (Maybe you will add others in another time). Like this:

$data[] = array(
    'User' => array(
        'username' => $username,
        'password' => $password
    ),
    'Profile' => array(
        0 => array(           //NEW LINE ADDED!!!!
            'name' => $name,
        )                     //NEW LINE ADDED!!!!

    )
);

Sorry for wasting your time. The question will keep alive just to help another guy with the same problem.

OTHER TIPS

Use save associated

$this->User->saveAssociated($data);

here is working code from my app:

Controller Code:

public function add() {


        if ($this->request->is('post')) {
            $this->Member->create();

            if ($this->Member->User->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The member has been saved.'));
                return $this->redirect($this->referer());
            } else {
                $this->Session->setFlash(__('The member could not be saved. Please, try again.'), 'flash_message_error');
            }
        }
                $countries = $this->Member->Country->fetchList();

        $title_for_layout = 'Membership Signup';
        $this->set(compact('title_for_layout', 'countries'));
    }

User Model Code:

  public $hasOne = 'Member';

Member Model Code:

 public $belongsTo = array(        

          'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

View Code:

<div class="row">
     <?php echo $this->Form->create('Member', array('url'=>array('controller'=>'members', 'action'=>'add', 'admin'=>false))); ?>

        <div class="col-md-12 col-sm-12 col-xs-12">
         <h3 class="sign-head">Personal Information</h3>
        </div>

                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <?php echo $this->Form->input('User.username', array('div'=>FALSE, 'required'=>TRUE, 'placeholder'=>'username')); ?>
                  </div>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <?php echo $this->Form->input('User.password', array('div'=>FALSE, 'required'=>TRUE, 'placeholder'=>'Password')); ?>
                  </div>
                  <div class="col-md-6 col-sm-6 col-xs-12">
                    <?php echo $this->Form->input('Member.first_name', array('div'=>FALSE, 'required'=>TRUE, 'placeholder'=>'Name')); ?>
                  </div>

                  <div class="col-md-12 col-sm-6 col-xs-12">
                     <?php echo $this->Form->submit('submit', array('div'=>FALSE, 'class'=>' submit-contact')); ?>
                  </div>

          <?php echo $this->Form->end(); ?>
                </div>

Hope this help to other user.

  $user = array(
        'username' => $username,
        'password' => $password

     );
     $this->User->save($user);

  $profile = array(
        'name' => $name);
     $this->Profile->save($profile);

you have to create a profile Model

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top