# How can I save multiple Orders with one Form?

**URL:** <https://discourse.cakephp.org/t/how-can-i-save-multiple-orders-with-one-form/6250>\
**Category:** Need Help\
**Created:** [June 11, 2019, 9:38am UTC](https://discourse.cakephp.org/t/how-can-i-save-multiple-orders-with-one-form/6250 "2019-06-11T09:38:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatWave](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PatWave](https://discourse.cakephp.org/u/PatWave)\
**Post date:** [June 11, 2019, 9:38am UTC](https://discourse.cakephp.org/t/how-can-i-save-multiple-orders-with-one-form/6250/1 "2019-06-11T09:38:07Z")

</div>

Hey everyone,

I want that to save the Customer (customer\_id) multiple times but with different date and menu\_item\_id.  
Example:

Customer: Max | Date: 11.06.2019 | menu\_item\_id: 1  
Same Customer but | Date: 10.06.2019 | menu\_item\_id: 3  
Still Same Customer | Date 09.06.2019 | menu\_item\_id: 5

This is what i want to do but i cant make it work.

My Orders add.ctp

```
<div class="customers index large-9 medium-8 columns content">
<?= $this->Form->create($order) ?>
<h3><?= __('Bestellung Tabelle') ?></h3>
<?php	
		echo $this->Form->control('customer_id', array('label' => __('Kunde', true)),['options' => $customers, 'empty' => false]);
		echo $this->Form->control('date', array('label' => __('Datum',true), 'empty' => true));   
        echo $this->Form->control('menu_item_id', array('label' => __('Menü',true), 'empty' => true)); 	
    ?>

        <?= $this->Form->button(__('Bestätigen')) ?>
<?= $this->Form->end() ?>

```

I tryed to just add another two of those:

```
 echo $this->Form->control('date', array('label' => __('Datum',true), 'empty' => true));   
 echo $this->Form->control('menu_item_id', array('label' => __('Menü',true), 'empty' => true)); 

```

so I have:

```
echo $this->Form->control('customer_id', array('label' => __('Kunde', true)),['options' => $customers, 'empty' => false]);
echo $this->Form->control('date', array('label' => __('Datum',true), 'empty' => true));   
echo $this->Form->control('menu_item_id', array('label' => __('Menü',true), 'empty' => true)); 	
echo $this->Form->control('date', array('label' => __('Datum',true), 'empty' => true));   
echo $this->Form->control('menu_item_id', array('label' => __('Menü',true), 'empty' => true)); 	

```

but they just overwrite the Inputs I entered before them.

So how can I save the same Customer with multiple Dates and Menus? Because I think i need to change something in my Controller but I am not sure what.

OrdersController.php (only the add function)

```
public function add()
{   
    $order = $this->Orders->newEntity();
    if ($this->request->is('post')) {
        $order = $this->Orders->patchEntity($order, $this->request->getData());
        if ($this->Orders->save($order)) {
            $this->Flash->success(__('The order has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The order could not be saved. Please, try again.'));
    }
    $customers = $this->Orders->Customers->find('list', ['limit' => 200]);
    $menuItems = $this->Orders->MenuItems->find('list', ['limit' => 200]);
    $this->set(compact('order', 'customers', 'menuItems'));
}

```

I read something about changing the newEntity() to newEntities() but I didnt understood how to do it with my code?

Im pretty new to coding in general only got some knowledge from cms systems, so I would be pretty happy if someone could help me to understood it better.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [June 11, 2019, 9:46am UTC](https://discourse.cakephp.org/t/how-can-i-save-multiple-orders-with-one-form/6250/2 "2019-06-11T09:46:18Z")

</div>

Might not be ideal but maybe use a `menuItems[]` field in the form and send that?  
Then use something like a for loop to parse it.

> <https://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php>

---

<div class="post-metadata">

**Author:** ![PatWave](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@PatWave](https://discourse.cakephp.org/u/PatWave)\
**Post date:** [June 24, 2019, 6:04pm UTC](https://discourse.cakephp.org/t/how-can-i-save-multiple-orders-with-one-form/6250/3 "2019-06-24T18:04:34Z")

</div>

So i found a [Solution](https://stackoverflow.com/questions/32912941/cakephp-3-x-saving-multiple-entities-newentities) to my problem. But now i have the Problem that I need to choose the Customer Multiple Times.

This is now my add.ctp

```
<div class="orders form large-9 medium-8 columns content">
<?= $this->Form->create($order) ?>

    <legend><?= __('Neue Bestellung') ?></legend>
    <?php
    	echo $this->Form->control('0.customer_id', array('label' => __('Kunde',true)));
        echo $this->Form->control('0.date', array('label' => __('Datum',true)));
        echo $this->Form->control('0.menu_item_id', array('label' => __('Menü',true)));
        echo $this->Form->control('1.customer_id', array('label' => __('Kunde',true))); 
        echo $this->Form->control('1.date', array('label' => __('Datum',true)));
        echo $this->Form->control('1.menu_item_id', array('label' => __('Menü',true)));
    ?>

<?= $this->Form->button(__('Bestätigen')) ?>
<?= $this->Form->end() ?>

```

With this i can save multliple Orders. What do I need to change so that I only need to input my customer\_id one time?

My OrdersController.php

```
public function add()
{
    
    $order = $this->Orders->newEntity();
    if ($this->request->is('post')) {
        $orders = $this->Orders->newEntities($this->request->getData());
        
        foreach ($orders as $order)
        {                
            $this->Orders->save($order);
        }
        $this->Flash->success(__('The order has been saved.'));
        
        return $this->redirect(['action' => 'index']);
        
        $this->Flash->error(__('The order could not be saved. Please, try again.'));
    }
    $customers = $this->Orders->Customers->find('list', ['limit' => 200]);
    $menuItems = $this->Orders->MenuItems->find('list', ['limit' => 200]);
    $this->set(compact('order', 'customers', 'menuItems'));
}

```

I really struggle to find a solution, that I only need to choose the customer one time, inside the form.
