/** * 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, false, true); $this->trigger(Events::SAVING, $metadata, [$entity, $data]); if (!($id = $metadata->getValue($entity, $identifier, true))) { $this->trigger(Events::CREATING, $metadata, [$entity, $data]); $this->connection->insert($metadata->getTable(), $metadata->getValues($entity, true, true)); $metadata->setValue($entity, $identifier, $this->connection->lastInsertId(), true, true); $this->trigger(Events::CREATED, $metadata, [$entity, $data]); } else { $this->trigger(Events::UPDATING, $metadata, [$entity, $data]); $this->connection->update($metadata->getTable(), $metadata->getValues($entity, true, true), [$identifier => $id]); $this->trigger(Events::UPDATED, $metadata, [$entity, $data]); } $this->trigger(Events::SAVED, $metadata, [$entity, $data]); }