# Understanding saveMany and validation errors

**URL:** <https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651>\
**Category:** Need Help\
**Created:** [April 14, 2020, 8:06pm UTC](https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651 "2020-04-14T20:06:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![met](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@met](https://discourse.cakephp.org/u/met)\
**Post date:** [April 14, 2020, 8:06pm UTC](https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651/1 "2020-04-14T20:06:23Z")

</div>

```
    $data = $this->MyModel->newEntities($theData);
    $result = $this->MyModel->saveMany($data);

```

`$theData` in this example contains a varying number of entities. If any of the entities fail validation. `saveMany` returns false, however the entities that passed validation still get saved.

In the context of my application this is somewhat beneficial as it saves data even if some of it is invalid, but I imagine this will lead to unpredictable results.

What is a recommended approach to dealing with a `saveMany` and is it good practice for entities with errors to be let slip through?

It seems like 2 very powerful functions doing a lot of work behind the scenes without much control.

I suppose what I’d like is for valid entities to be saved, then the user to get a message…

```
5 records were invalid - reasons:
1. bla bla
2. bla bla
....

```

Right now my app either saves all entities perfectly, or saves some and returns false, which makes it quite difficult to provide further information.

Thanks for any insight.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 14, 2020, 10:25pm UTC](https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651/2 "2020-04-14T22:25:16Z")

</div>

What database are you using, and does the table you’re writing to support transactions? `saveMany` should happen in a transaction, and roll back all saves if any fail.

---

<div class="post-metadata">

**Author:** ![met](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@met](https://discourse.cakephp.org/u/met)\
**Post date:** [April 15, 2020, 7:41am UTC](https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651/3 "2020-04-15T07:41:47Z")

</div>

Thanks for the reply - I think this might have been resolved in 4.0.5

> **[CakePHP 4.0.5 Released — Bakery](https://bakery.cakephp.org/2020/03/28/cakephp_405_released.html)**
>
> Latest news and articles about CakePHP

> “Table::saveMany() now correctly rollbacks a transaction when an entity other than the first fails to save because of application rules or database failure.”

I will update and see - I’m pretty sure my version of MySQL (5.0.2) does, it’s using the InnoDB Storage Engine.

That said - what is an appropriate way of handling errors with `saveMany`? I tried debugging `getErrors` and `hasErrors` but it just throws `Call to a member function hasErrors() on array`

```
$data = $this->MyModel->newEntities($theData);
debug($data->getErrors()); // here?
$result = $this->MyModel->saveMany($data);

```

---

<div class="post-metadata">

**Author:** ![met](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@met](https://discourse.cakephp.org/u/met)\
**Post date:** [April 15, 2020, 9:09am UTC](https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651/4 "2020-04-15T09:09:54Z")

</div>

Yep updating to 4.0.5 fixed it. I’m using:

```
    $myData = $this->MyModel->newEntities($theData);
    foreach($myData as $k => $v) {
        if($v->hasErrors()) {
            unset($myData[$k]);
        }
    }
    $result = $this->MyModel->saveMany($myData);

```

To remove entities that aren’t valid, which leaves valid entities and it saves perfectly.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 15, 2020, 3:43pm UTC](https://discourse.cakephp.org/t/understanding-savemany-and-validation-errors/7651/5 "2020-04-15T15:43:11Z")

</div>

Note that what you have will remove entities that fail validation. But _rules_ are not run until it tries to save, so it’s still possible that what you have here will fail. What you want in this case may not be `saveMany`, but rather just your own loop that tries to save each one separately and ignores any errors that may happen with each one.
