Add HABTM data to the controller

In my UsersHABTM app Solicitations.

After saving the form with, $this->Solicitation->save($this->request->data)I need to add another value user_idto the query table.

$data[] = array('User' => array('id' => 5), 'Solicitation' => array('id' => 77));
$data[] = array('User' => array('id' => 6), 'Solicitation' => array('id' => 77));

  $this->Solicitation->saveMany($data);

My $data:

array(
'User' => array(
    'id' => (int) 6
),
'Solicitation' => array(
    'id' => '54'
)
)

I need to save the association on the form, and then add the new record above to the table solicitations_users. It saves only 6, not the data from the form. It just keeps the form if I delete the second one save.

I realized that in the DB this jumps alone id. It should be 'id' 36 => 5; 'id' 37 => 6. This seems to be a table update.

Here is the request:

enter image description here

+4
source share
5 answers

Thanks guys.

: CakePHP: HABTM

, SaveAll , , . - query() .

$this->Solicitation->query("INSERT INTO `contratos`.`solicitation_users` (user_id,solicitation_id) values($id, $solicitation");

-1

, ?

cookbook

Array(
    'User' => Array(
        'id' => 6
    ),
    'Solicitation' => Array(
        'title' => 'A request'
    )
)
+1

habtm, thet , .

.

, , , :

$data = array(
    'Solicitation' => array('id' => 77),
    'User' => array(
        'User' => array(6, 7)
    )
);

$this->Solicitation->saveAll($data);

'unique' =>'keepExisting' habtm

+1

:

$data[] = array('User' => array('id' => 5), 'Solicitation' => array('id' => 77));
$data[] = array('User' => array('id' => 6), 'Solicitation' => array('id' => 77));

$this->Solicitation->saveMany($data, array('deep' => true));

$data[] = array('SolicitationsUser' => array('User.id' => 5,'Solicitation.id' => 77));
$data[] = array('SolicitationsUser' => array('User.id' => 6,'Solicitation.id' => 77));

$this->Solicitation->saveMany($data);
+1

, - foreach. -

$data[] = array('User' => array('id' => 5), 'Solicitation' => array('id' => 77));
$data[] = array('User' => array('id' => 6), 'Solicitation' => array('id' => 77));

foreach($data as $da){
  $this->Solicitation->saveMany($da);
}
+1
source

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


All Articles