Patching records using serveral input fields per record not working

I need to Patch many records using serveral input fields, like for price and reduced price

Products View - Index

<?= $this->Form->create(null, ['url' => ['controller' => 'Products', 'action' => 'index']]); ?>
  <?php foreach ($products as $key => $product): ?>
     <?php echo $this->Form->control("{$key}.id", ['label' => false, 'value' => $product->id]); ?>
      <?php echo $this->Form->control("{$key}.reduced_price", ['label' => false, 'value' => $product->reduced_price]); ?>
      <?php echo $this->Form->control("{$key}.price", ['label' => false, 'value' => $product->price]); ?>
  <?php endforeach; ?>
<?= $this->Form->end(); ?>

ProductsController

public function index()
{
  if ($this->getRequest()->is('post')) {
    $this->Products->updateProducts($this->getRequest()->getData());
  }
  // some more actions
}

ProductsTable

public function updateProducts(array $entries)
{
  $products = $this->find()->toArray();
  $entities = $this->patchEntities($products, $entries);
  return $this->saveMany($entities);
}

Array output

0 => array:3 [▼
    "id" => 7319
    "reduced_price" => "9870"
    "price" => "2009"
  ]
1 => array:3 [▼
    "id" => 7318
    "reduced_price" => "875785"
    "price" => "299.5"
 ]
  ...

But nothing happens.

So what i’m doing wrong here?

You need to pass the $products as context using the first argument of FormHelper::create() so $this->Form->create($products, ...).

I tried that, but it did not work for me. Maybe saveMany() Method fails because there are to many Model Associations.

But this Solution works for me:

At Controller

if ($this->getRequest()->is('post')) {
  $products = $this->Products->find()->toList();
  $patches = $this->Products->patchEntities($products, $this->request->getData());

  $this->Products->getConnection()->transactional(function () use ($products, $patches) {
     foreach ($patches as $patch) {
       $this->Products->save($patch, ['atomic' => false]);
     }
  });

 $this->redirect($this->referer());
}

You’re not checking the results of any of your single-entity saves. Any chance that one (or more) of them are failing? That would cause the saveMany to not save anything, I think.

according to docs Saving Data - 4.x saveMany() should return entities or false, in your case it seems like array not object. Try to debug $entities after patch and check for errors and check if they are entities there.
You can also look for SQL history in debug bar to check if there was any saves.

I’m using mostly 3.1 version and there was no saveMany() but there was patchEntities() and it was working for many rows forms, using convention like @ADmad propose.