Пример #1
0
 /**
  * Saves an entity.
  *
  * @param object $entity
  * @param array  $data
  */
 public function save($entity, array $data = [])
 {
     $metadata = $this->getMetadata($entity);
     $identifier = $metadata->getIdentifier(true);
     $metadata->setValues($entity, $data);
     $this->dispatchEvent(Events::preSave, $entity, $metadata);
     switch ($this->getEntityState($entity, self::STATE_NEW)) {
         case self::STATE_NEW:
             $this->dispatchEvent(Events::preCreate, $entity, $metadata);
             $this->connection->insert($metadata->getTable(), $metadata->getValues($entity, true, true));
             $this->entities->add($entity, $id = $this->connection->lastInsertId());
             $metadata->setValue($entity, $identifier, $id, true);
             $this->dispatchEvent(Events::postCreate, $entity, $metadata);
             break;
         case self::STATE_MANAGED:
             $this->dispatchEvent(Events::preUpdate, $entity, $metadata);
             $values = $metadata->getValues($entity, true, true);
             $this->connection->update($metadata->getTable(), $values, [$identifier => $values[$identifier]]);
             $this->dispatchEvent(Events::postUpdate, $entity, $metadata);
     }
     $this->dispatchEvent(Events::postSave, $entity, $metadata);
 }