Я очень новичок в Cakephp 3. Я пытаюсь сделать функцию saveAll в Cakephp 3, но безуспешно.
У меня есть 2 таблицы: продавцы и пользователи
Отношения между этими двумя таблицами, как показано ниже:
У пользователей много продавцов, продавцы принадлежат пользователям
Я хочу создать регистрационную форму продавца, которая будет вставлять данные в обе таблицы.
Может ли кто-нибудь помочь мне, как вставить данные в 2 разные таблицы из 1 формы с помощью Cakephp 3?
Мой код, как показано ниже:
<?= $this->Form->create($merchant); ?>
<fieldset>
<legend><?= __('Add Merchant') ?></legend>
<?php
echo $this->Form->input('User.id');
echo $this->Form->input('User.username');
echo $this->Form->input('User.password');
echo $this->Form->input('merchant_type_id', ['options' => $merchantTypes]);
echo $this->Form->input('name');
echo $this->Form->input('identity_number');
echo $this->Form->input('phone_number');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Контроллер:
public function registration()
{
$merchant = $this->Merchants->newEntity();
if ($this->request->is('post')) {
$merchant = $this->Merchants->patchEntity($merchant, $this->request->data, [
'associated' => ['Users']
]);
if ($this->Merchants->saveAll($merchant)) {
$this->Flash->success('The merchant has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The merchant could not be saved. Please, try again.');
}
}
$users = $this->Merchants->Users->find('list', ['limit' => 200]);
$merchantTypes = $this->Merchants->MerchantTypes->find('list', ['limit' => 200]);
$this->set(compact('merchant', 'users', 'merchantTypes'));
}
Ваша помощь очень ценится. Спасибо.
saveAll()
. book.cakephp.org /3.0/ru/orm/saving-data.html - person ndm   schedule 02.03.2015save()
вместоsaveAll()
. - person Supravat Mondal   schedule 02.03.2015