/** * Insert an entity. * * @param object $entity The entity to store * * @return void * @throws OrmException */ public function insert($entity) { $entityId = $this->entityRegistry->getEntityId($entity); if (empty($entityId)) { $id = 0; foreach ($this->gateway->getAll($this->tableName) as $row) { $id = max($id, $row['id']); } $this->entityRegistry->setEntityId($entity, $id + 1); } $this->gateway->insert($this->tableName, $this->builder->reduce($entity)); $this->builder->resolve($entity); }
/** * Insert an entity. * * @param object $entity The entity to store * * @return void */ public function insert($entity) { $data = $this->builder->reduce($entity); $entityId = $this->entityRegistry->getEntityId($entity); try { $this->connection->insert($this->tableName, $data); if (empty($entityId)) { $this->entityRegistry->setEntityId($entity, $this->connection->lastInsertId()); } $this->builder->resolve($entity); } catch (\Exception $e) { throw new OrmException("Entity with id {$entityId} already exists.\n" . $e->getMessage(), 0, $e); } }