/** * 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); }
/** * Sets an option value. * * @param string $name * @param mixed $value * @param boolean $autoload * @throws \InvalidArgumentException */ public function set($name, $value, $autoload = null) { $name = trim($name); if (empty($name)) { throw new \InvalidArgumentException('Empty option name given.'); } if (in_array($name, $this->protected)) { throw new \InvalidArgumentException(sprintf('"%s" is a protected option and may not be modified.', $name)); } if (!$this->connection->isConnected()) { throw new \RuntimeException('Database is not connected.'); } $old_value = $this->get($name); if ($value !== $old_value) { $this->options[$name] = $value; $data = ['name' => $name, 'value' => json_encode($value)]; if ($autoload !== null) { $data['autoload'] = $autoload ? '1' : '0'; } if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { if ($autoload === null) { $query = "INSERT INTO {$this->table} (name, value) VALUES (:name, :value) ON DUPLICATE KEY UPDATE value = :value"; } else { $query = "INSERT INTO {$this->table} (name, value, autoload) VALUES (:name, :value, :autoload) ON DUPLICATE KEY UPDATE value = :value, autoload = :autoload"; } $this->connection->executeQuery($query, $data); } elseif (!$this->connection->update($this->table, $data, compact('name'))) { $this->connection->insert($this->table, $data); } $this->cache->delete($this->prefix . (isset($this->autoload[$name]) ? 'Autoload' : $name)); } if (isset($this->ignore[$name])) { unset($this->ignore[$name]); $this->cache->save($this->prefix . 'Ignore', $this->ignore); } }