0
votes

I am very new to Cakephp 3. I try to do saveAll function in Cakephp 3, but unsuccessful.

I have 2 tables : Merchants and Users

Relationship between this 2 tables as below:

Users hasMany Merchants, Merchants belongsTo Users

I want the create a merchant's registration form that will insert the data to both tables.

Can someone help me how to insert data into 2 different tables from 1 form using Cakephp 3?

My code as below:

 <?= $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() ?>

Controller:

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'));
}

Your helps is very appreciated. Thanks.

2
What is the relationship between Merchants and Users? For saveAll to work the Models must be related, so you need Merchant hasMany User or Merchant hasAndBelongsToMany User or the other way around.Simon Mason
There is no saveAll() in CakePHP 3. book.cakephp.org/3.0/en/orm/saving-data.htmlndm
hi @ndm , yes i have read the documentation. Do we have another way to save data into 2 different table? I tried put '$merchant = $this->Merchants->patchEntity($merchant, $this->request->data, [ 'associated' => ['Users'] ]);" but unsuccessful.batrisya
hi @SimonMason, i have updated the relationship in my question above. Yes, the table got the relationship.batrisya
Hi @Debasis, I have update the code in my question.batrisya

2 Answers

2
votes

I think your problem is that your fields aren't named by lower-underscored version of the association name.

Instead of put:

echo $this->Form->input('User.username')

Which would result:

<input type="text" name="User[username]" >

You should try to put something as:

echo $this->Form->input('user.username');

Which would result:

<input type="text" name="user[username]" >

I hope it would be useful :)

PS: Sorry if there's any grammar mistakes but English is not my native language ;)

0
votes

Please follow the documentation for saving data:

http://book.cakephp.org/3.0/en/orm/saving-data.html

If you are coming from 2.x, read the ORM migration guide, it should answer most of your questions:

http://book.cakephp.org/3.0/en/appendices/orm-migration.html