Bolt\Storage\Repository::save PHP Method

save() public method

Saves a single object.
public save ( object $entity, boolean $silent = null ) : boolean
$entity object The entity to save.
$silent boolean Suppress events
return boolean
    public function save($entity, $silent = null)
    {
        try {
            if ($existing = $entity->getId()) {
                $creating = false;
            } else {
                $creating = true;
            }
        } catch (\Exception $e) {
            $creating = $existing = false;
        }
        if ($silent === null) {
            $event = new StorageEvent($entity, ['create' => $creating]);
            $this->event()->dispatch(StorageEvents::PRE_SAVE, $event);
        }
        if ($existing) {
            $response = $this->update($entity);
        } else {
            $response = $this->insert($entity);
        }
        if ($silent === null) {
            $this->event()->dispatch(StorageEvents::POST_SAVE, $event);
        }
        return $response;
    }

Usage Example

Example #1
0
 /**
  * {@inheritdoc}
  */
 public function save($entity, $silent = null)
 {
     $this->userEntities = [];
     return parent::save($entity, $silent);
 }