Cake 3.7: Adding multiple records, one at a time


#1

Hi all.
I have one of my customers wich regularly receives deliveries of fabric rolls and needs to keep track of them.
Each delivery contains N rolls, all from the same manufacturer and with the same height, length and fabric type.
Each roll has its own serial number, printed on a label.

My first thought was to design a form where the user enters the common properties and then all the serial numbers, one by row.
Unfortunately this idea has been discarded because the customer’s warehouse chief wants to add the rolls one by one (basically he wants a visual and acoustic confirmation after every roll has been added successfully).

So i tried to design an action where the form gets pre-filled with the same data as the latest successfull save.

This is what i came up with:

public function addMulti()
{
$roll = $this->Rolls->newEntity();

if ($this->request->is(['post', 'put'])) {
    $roll = $this->Rolls->patchEntity($roll, $this->request->getData());
    if ($this->Rolls->save($roll)) {
        $this->Flash->success(__('Roll successfully added'));
        $this->id = $roll->serial_number = null;
        $roll->clear();
    } else {
        $this->Flash->error(__('Unable to add roll'));
    }
}

$this->set('roll', $roll);
$this->set('manufacturers', $this->Rolls->Manufacturers->find('list'));
$this->set('fabrics', $this->Rolls->Fabrics->find('list'));

}

And here’s the ‘add_multi’ template

echo $this->Form->create();

echo $this->Form->control(‘serial_number’, [‘autofocus’ => ‘autofocus’]);

echo $this->Form->control(‘manufacturer_id’, [
‘options’ => $manufacturers
]);

echo $this->Form->control(‘fabric_id’, [
‘options’ => $fabrics
]);

echo $this->Form->control(‘height’);
echo $this->Form->control(‘length’);

echo $this->Form->submit();
echo $this->Form->end();

But it’s not working.
The ‘serial_number’ form control keeps the submitted value and i’m not able to understand why.
I even tried messing up with the $_POST variable and request->data but so far i’ve not been able to acheive what my customer wants.

Am i missing something?

Sorry for the long post and for the bad english.

Thank you.


#2

Try

echo $this->Form->create($roll);

Without providing an entity there, it will look to what was posted, and it might be looking somewhere that you didn’t reset in your attempts.


#3

Yeah, that was a mistake wich i corrected immediately after starting this thread.
Nothing changes, even with the entity passed to FormHelper

Thank you