0

I am a beginner of CakePHP and I'm using version 3.5. I really get confused by it. My problem is I want to merge two forms from 2 different models. In my case, I want to merge the client transaction add form and service detail add form all in one form. Here are my codes below:

add.ctp (I merge the add client transaction form in service detail add form)

<div class="serviceDetail form large-9 medium-8 columns content">
<?= $this->Form->create($clientTransaction) ?>
<fieldset>
    <legend><?= __('Add Client Transaction') ?></legend>
    <?php
        echo $this->Form->control($serviceDetail->client_transaction->client->id, ['options' => $clients]);
        echo $this->Form->control($serviceDetail->client_transaction->client->transaction_date, ['timeFormat' => 12, 'disabled' => false]);
        echo $this->Form->control($serviceDetail->client_transaction->client->status, ['type' => 'hidden', 'value' => 0]);
    ?>
</fieldset>
<?= $this->Form->create($serviceDetail) ?>
<fieldset>
    <legend><?= __('Add Service Detail') ?></legend>
    <?php
        echo $this->Form->control('service_id', ['options' => $service,  'empty' => true]);
        echo $this->Form->control('user_id', ['options' => $users, 'empty' => true, 'label'=> ['text' => 'Barber']]);
        echo $this->Form->control('client_transaction_id', ['options' => $clientTransaction, 'empty' => true]);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

in my service_detail controller

public function add()
{
    $this->paginate = [
        'contain' => ['Service', 'Users', 'ClientTransaction']
    ];
    $clientTransaction = $this->ServiceDetail->ClientTransaction->newEntity();
    $serviceDetail = $this->ServiceDetail->newEntity();
    if ($this->request->is('post')) {
        $clientTransaction = $this->ServiceDetail->ClientTransaction->patchEntity($clientTransaction, $this->request->getData());
        $serviceDetail = $this->ServiceDetail->patchEntity($serviceDetail, $this->request->getData());
        if ($this->ServiceDetail->ClientTransaction->save($clientTransaction) && $this->ServiceDetail->save($serviceDetail)) {
            $this->Flash->success(__('The transaction has been added.'));

            return $this->redirect(['action' => 'add']);
        }
        $this->Flash->error(__('Please, try again.'));
    }
    $service = $this->ServiceDetail->Service->find('list', ['limit' => 200]);
    $users = $this->ServiceDetail->Users->find('list',['limit' => 200, 'valueField' => 'first_name'])->where(['position' => 2]);
    $clientTransaction = $this->ServiceDetail->ClientTransaction->find('list', ['limit' => 200]);
    $clients = $this->ServiceDetail->ClientTransaction->Clients->find('list', ['limit' => 200]);
    $serviceDetail = $this->paginate($this->ServiceDetail);
    $this->set(compact('serviceDetail', 'service', 'users', 'clientTransaction'));
}
halfer
  • 19,824
  • 17
  • 99
  • 186
boldsinas101
  • 300
  • 2
  • 5
  • 22
  • 1
    This has been answered many times here in the past. Read the manual section on [associations](https://book.cakephp.org/3.0/en/orm/associations.html), and search for associations here. – Greg Schmidt Feb 18 '18 at 16:04
  • Can you please mention the relation between client_transaction ans service_detail table, Means hasOne or hasMany or belongsTo? – bikash.bilz Feb 19 '18 at 01:59
  • client_transaction has many service detail. The problem is I want to merge it one add.ctp form. There is an error appeared. Now I'm confused on how to do it – boldsinas101 Feb 19 '18 at 06:29

1 Answers1

0

Do something like this:

$serviceDetail = $this->ServiceDetail->newEntity(
    $this->request->getData(), [
        'associated' => ['ClientTransaction']
    ]
);

https://book.cakephp.org/3.0/en/orm/saving-data.html#converting-request-data-into-entities

For your page follow this pattern: https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data

Syan Souza
  • 149
  • 12