# How to detect an error when saving

**URL:** <https://discourse.cakephp.org/t/how-to-detect-an-error-when-saving/646>\
**Category:** Uncategorized\
**Created:** [June 29, 2016, 5:05am UTC](https://discourse.cakephp.org/t/how-to-detect-an-error-when-saving/646 "2016-06-29T05:05:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jagguy](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@jagguy](https://discourse.cakephp.org/u/jagguy)\
**Post date:** [June 29, 2016, 5:05am UTC](https://discourse.cakephp.org/t/how-to-detect-an-error-when-saving/646/1 "2016-06-29T05:05:04Z")

</div>

Hi,  
I am saving multiple records. The code save multiple records but I dont understand how I can detect an error with saving in cakephp3.  
The check I have in place will always return true I believe.

```
 if (!empty($data)){
       $lessons = $this->Lessons->newEntities($data, ['validate' => false]);
 
       //saves all in 1 transaction instead of 1 by 1
       $this->Lessons->connection()->transactional(function () use ( $lessons) {
       foreach ($lessons as $item) {
           $result=$this->Lessons->save($item, ['atomic' => false]);
          if ( $result ){ //this is the check for error when saving
            
           
             $this->Flash->success(__('A lesson has been saved on .'. $item->lesson_date));
             $last_record_id=$result->id;
          
          }
          else{
              $this->Flash->error(__('There has been an error saving a lesson. Please, try again.'));
          }
          
       }
   });
  
  }//if not empty
```

---

<div class="post-metadata">

**Author:** ![alysson\_azevedo](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/alysson_azevedo/32/194_2.png) [@alysson\_azevedo](https://discourse.cakephp.org/u/alysson_azevedo)\
**Post date:** [June 29, 2016, 11:39pm UTC](https://discourse.cakephp.org/t/how-to-detect-an-error-when-saving/646/2 "2016-06-29T23:39:01Z")

</div>

I think `$this->Lessons->save()` returns false when there’s error. If it is false, use `$item->errors()` to see the errors.

When there’s no error, `$this->Lessons->save` returns the saved entity. In this case, `$result === $item`.
