Saving with HABTM in CakePHP

I create several associations at a time, and there are several problems when it comes to saving.

I have the following code:

<?php
foreach($userData as $user) {
    $data = array('User' => array('id' => $user['id']), 'Site' => array('id' => $user['site_id']));
    $this->User->save($data);
}
?>

I experimented with formatting the data array in different ways, although I always run into the same problems. Either previous records are moved when a new one is inserted, or the current one is updated.

I could just use the following, although I need the behavior to run.

$this->User->SiteUser->save($data);

Edit: Also $ this-> User-> create (); doesn't seem to be doing much.

+3
source share
2 answers

IRC , , false, .

//In the user model
var $hasAndBelongsToMany = array(
  'Site' => array(
    'className' => 'Site',
    'unique' => false
  )
);
+8

id save(), , :

$this->User->id = null;

Cake , id, save() id. , create() .

, HABTM, saveAll() save(). . .

+1

Source: https://habr.com/ru/post/1713918/


All Articles