/** * Select's constructor * * @param PDO $pdo Pdo instance * @param Mapper $mapper the Mapper * @param array $query a query on '[key=>value,key=>value,..]' style * * @throws ErrorException */ public function __construct(PDO $pdo, Mapper $mapper, array $query = []) { $map = $mapper->getMap(); $selectFields = []; $properties = $map->getProperties(); foreach ($properties as $alias => $field) { $selectFields[] = "{$field} as `{$alias}`"; } $select = 'SELECT ' . implode(', ', $selectFields); $from = 'FROM ' . $map->getRemoteName(); $whereParameters = []; $queryParameters = array_keys($query); foreach ($queryParameters as $key) { $whereParameters[] = "{$properties[$key]} = :{$key}"; } $where = ''; if ($whereParameters) { $where = 'WHERE ' . implode(' AND ', $whereParameters); } $sql = implode(' ', [$select, $from, $where]); $statement = $pdo->prepare($sql); if (!$statement) { throw new ErrorException('A SQL hasn\'t been generated: [' . $sql . ']'); } $statement->setFetchMode(PDO::FETCH_CLASS, $map->getLocalName()); foreach ($query as $field => $value) { $statement->bindValue(':' . $field, $value); } $statement->execute(); $this->pdoStatement = $statement; }
/** * Persists $object * * @param Mapper $mapper Mapper * @param object $object Object to be saved * * @return Insert|Update */ public function save(Mapper $mapper, $object) { $identifier = $mapper->getMap()->getIdentifier(); $data = $mapper->getMap()->getData($object); if ($data[$identifier]) { return $this->update($mapper, $data); } return $this->insert($mapper, $object, $data); }
/** * Delete action. * * @param PDO $pdo * @param \Drakojn\Io\Mapper $mapper * @param Object $object * * @return \Drakojn\Io\Driver\SQL\Delete */ public function __construct(PDO $pdo, Mapper $mapper, $object) { $map = $mapper->getMap(); $identifier = $map->getIdentifier(); $remoteIdentifier = $map->getProperties()[$identifier]; $data = $map->getData($object); $delete = 'DELETE FROM ' . $map->getRemoteName(); $where = 'WHERE ' . $remoteIdentifier . ' = :' . $identifier; $this->sql = "{$delete} {$where}"; $this->statement = $pdo->prepare($this->sql); $this->statement->bindValue(':' . $identifier, $data[$identifier]); }
/** * @covers Drakojn\Io\Mapper::delete */ public function testDelete() { $all = $this->object->find([]); $ourPick = $all[rand(0, count($all) - 1)]; $ourClone = clone $ourPick; $return = $this->object->delete($ourPick); $this->assertTrue((bool) $return); $try = $this->object->find(['id' => $ourClone->getId()]); $this->assertEmpty($try); }
/** * Constructor. * * @param PDO $pdo * @param \Drakojn\Io\Mapper $mapper * @param Object $object * @param array $data * * @return bool * * @throws ErrorException */ public function __construct(PDO $pdo, Mapper $mapper, $object, array $data) { $map = $mapper->getMap(); $properties = $map->getProperties(); $identifier = $map->getIdentifier(); unset($properties[$identifier]); $insert = 'INSERT INTO ' . $map->getRemoteName(); $columns = '(' . implode(', ', $properties) . ')'; $values = 'VALUES (:' . implode(', :', array_keys($properties)) . ')'; $sql = implode(' ', [$insert, $columns, $values]); $statement = $pdo->prepare($sql); if (!$statement) { throw new ErrorException('A SQL hasn\'t been generated: [' . $sql . ']'); } unset($data[$identifier]); foreach ($data as $field => $value) { $statement->bindValue(':' . $field, $value); } $this->statement = $statement; $this->map = $map; $this->pdo = $pdo; $this->object = $object; }
/** * Update's constructor * * @param PDO $pdo a Pdo instance * @param Mapper $mapper a Mapper instance * @param array $data data to persisted * * @throws ErrorException */ public function __construct(PDO $pdo, Mapper $mapper, array $data) { $map = $mapper->getMap(); $properties = $map->getProperties(); $identifier = $map->getIdentifier(); $remoteIdentifier = $properties[$identifier]; unset($properties[$identifier]); $update = 'UPDATE ' . $map->getRemoteName(); $fields = []; foreach ($properties as $value => $field) { $fields[] = "{$field} = :{$value}"; } $set = 'SET ' . implode(', ', $fields); $where = "WHERE {$remoteIdentifier} = :{$identifier}"; $sql = implode(' ', [$update, $set, $where]); $this->statement = $pdo->prepare($sql); if (!$this->statement) { throw new ErrorException(sprintf('A SQL hasn\'t been generated: [%s]', $sql)); } foreach ($data as $field => $value) { $this->statement->bindValue(':' . $field, $value); } }
/** * @param Mapper $mapper * @param mixed $object * * @return bool */ public function delete(Mapper $mapper, $object) { $map = $mapper->getMap(); $reflectionProperty = new \ReflectionProperty($map->getLocalName(), $map->getIdentifier()); $reflectionProperty->setAccessible(true); $identifier = $reflectionProperty->getValue($object); $uri = $this->buildUri($map->getRemoteName() . '/' . $identifier); if (!file_exists($uri)) { return true; } if ($this->context) { return unlink($uri, $this->context); } return unlink($uri); }