formatting and minor fixes

This commit is contained in:
Dennis Eichhorn 2021-11-29 20:03:21 +01:00
parent 77f099559b
commit e740bfbe3e
7 changed files with 30 additions and 22 deletions

View File

@ -31,6 +31,8 @@ abstract class DataMapperAbstract
protected int $type = 0; protected int $type = 0;
protected int $depth = 1;
protected array $with = []; protected array $with = [];
protected array $sort = []; protected array $sort = [];

View File

@ -158,6 +158,13 @@ class DataMapperFactory
{ {
} }
public static function db(ConnectionAbstract $db = null) : string
{
self::$db = $db;
return static::class;
}
public static function reader(ConnectionAbstract $db = null) : ReadMapper public static function reader(ConnectionAbstract $db = null) : ReadMapper
{ {
return new ReadMapper(new static(), $db ?? self::$db); return new ReadMapper(new static(), $db ?? self::$db);

View File

@ -68,7 +68,7 @@ class DeleteMapper extends DataMapperAbstract
private function deleteModel(object $obj, mixed $objId, \ReflectionClass $refClass = null) : void private function deleteModel(object $obj, mixed $objId, \ReflectionClass $refClass = null) : void
{ {
$query = new Builder(self::$db); $query = new Builder($this->db);
$query->delete() $query->delete()
->from($this->mapper::TABLE) ->from($this->mapper::TABLE)
->where($this->mapper::TABLE . '.' . $this->mapper::PRIMARYFIELD, '=', $objId); ->where($this->mapper::TABLE . '.' . $this->mapper::PRIMARYFIELD, '=', $objId);
@ -112,7 +112,7 @@ class DeleteMapper extends DataMapperAbstract
} }
} }
$sth = self::$db->con->prepare($query->toSql()); $sth = $this->db->con->prepare($query->toSql());
if ($sth !== false) { if ($sth !== false) {
$sth->execute(); $sth->execute();
} }
@ -235,19 +235,19 @@ class DeleteMapper extends DataMapperAbstract
{ {
if (empty($objsIds) if (empty($objsIds)
|| $this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::TABLE || $this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::TABLE
|| $this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::HAS_MANY[$propertyName]['mapper']::$table || $this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::HAS_MANY[$propertyName]['mapper']::TABLE
) { ) {
return; return;
} }
foreach ($objsIds as $src) { foreach ($objsIds as $src) {
$relQuery = new Builder(self::$db); $relQuery = new Builder($this->db);
$relQuery->delete() $relQuery->delete()
->from($this->mapper::HAS_MANY[$propertyName]['table']) ->from($this->mapper::HAS_MANY[$propertyName]['table'])
->where($this->mapper::HAS_MANY[$propertyName]['table'] . '.' . $this->mapper::HAS_MANY[$propertyName]['external'], '=', $src) ->where($this->mapper::HAS_MANY[$propertyName]['table'] . '.' . $this->mapper::HAS_MANY[$propertyName]['external'], '=', $src)
->where($this->mapper::HAS_MANY[$propertyName]['table'] . '.' . $this->mapper::HAS_MANY[$propertyName]['self'], '=', $objId, 'and'); ->where($this->mapper::HAS_MANY[$propertyName]['table'] . '.' . $this->mapper::HAS_MANY[$propertyName]['self'], '=', $objId, 'and');
$sth = self::$db->con->prepare($relQuery->toSql()); $sth = $this->db->con->prepare($relQuery->toSql());
if ($sth !== false) { if ($sth !== false) {
$sth->execute(); $sth->execute();
} }

View File

@ -27,7 +27,6 @@ use phpOMS\Utils\ArrayUtils;
*/ */
class ReadMapper extends DataMapperAbstract class ReadMapper extends DataMapperAbstract
{ {
private int $depth = 1;
private $columns = []; private $columns = [];
public function get() : self public function get() : self

View File

@ -81,7 +81,7 @@ class UpdateMapper extends DataMapperAbstract
return; return;
} }
$query = new Builder(self::$db); $query = new Builder($this->db);
$query->update($this->mapper::TABLE) $query->update($this->mapper::TABLE)
->where($this->mapper::TABLE . '.' . $this->mapper::PRIMARYFIELD, '=', $objId); ->where($this->mapper::TABLE . '.' . $this->mapper::PRIMARYFIELD, '=', $objId);
@ -140,7 +140,7 @@ class UpdateMapper extends DataMapperAbstract
} }
try { try {
$sth = self::$db->con->prepare($query->toSql()); $sth = $this->db->con->prepare($query->toSql());
if ($sth !== false) { if ($sth !== false) {
$sth->execute(); $sth->execute();
} }
@ -236,13 +236,13 @@ class UpdateMapper extends DataMapperAbstract
$relMapper->execute($value); $relMapper->execute($value);
$objsIds[$propertyName][$key] = $value; $objsIds[$propertyName][$key] = $primaryKey;
continue; continue;
} }
// create if not existing // create if not existing
if ($this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::HAS_MANY[$propertyName]['mapper']::$table if ($this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::HAS_MANY[$propertyName]['mapper']::TABLE
&& isset($mapper::COLUMNS[$this->mapper::HAS_MANY[$propertyName]['self']]) && isset($mapper::COLUMNS[$this->mapper::HAS_MANY[$propertyName]['self']])
) { ) {
$relProperty = $relReflectionClass->getProperty($mapper::COLUMNS[$this->mapper::HAS_MANY[$propertyName]['self']]['internal']); $relProperty = $relReflectionClass->getProperty($mapper::COLUMNS[$this->mapper::HAS_MANY[$propertyName]['self']]['internal']);
@ -291,15 +291,15 @@ class UpdateMapper extends DataMapperAbstract
$sth->execute(); $sth->execute();
$result = $sth->fetchAll(\PDO::FETCH_COLUMN); $result = $sth->fetchAll(\PDO::FETCH_COLUMN);
$removes = \array_diff($result, \array_keys($objsIds[$member] ?? [])); $removes = \array_diff($result, \array_values($objsIds[$member] ?? []));
$adds = \array_diff(\array_keys($objsIds[$member] ?? []), $result); $adds = \array_diff(\array_values($objsIds[$member] ?? []), $result);
if (!empty($removes)) { if (!empty($removes)) {
$this->mapper::remover()->deleteRelationTable($member, $removes, $objId); $this->mapper::remover(db: $this->db)->deleteRelationTable($member, $removes, $objId);
} }
if (!empty($adds)) { if (!empty($adds)) {
$this->mapper::writer()->createRelationTable($member, $adds, $objId); $this->mapper::writer(db: $this->db)->createRelationTable($member, $adds, $objId);
} }
} }
} }

View File

@ -74,7 +74,7 @@ class WriteMapper extends DataMapperAbstract
private function createModel(object $obj, \ReflectionClass $refClass) : mixed private function createModel(object $obj, \ReflectionClass $refClass) : mixed
{ {
$query = new Builder(self::$db); $query = new Builder($this->db);
$query->into($this->mapper::TABLE); $query->into($this->mapper::TABLE);
foreach ($this->mapper::COLUMNS as $column) { foreach ($this->mapper::COLUMNS as $column) {
@ -128,7 +128,7 @@ class WriteMapper extends DataMapperAbstract
} }
try { try {
$sth = self::$db->con->prepare($query->toSql()); $sth = $this->db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
} catch (\Throwable $t) { } catch (\Throwable $t) {
\var_dump($t->getMessage()); \var_dump($t->getMessage());
@ -136,7 +136,7 @@ class WriteMapper extends DataMapperAbstract
return -1; return -1;
} }
$objId = empty($id = $this->mapper::getObjectId($obj, $refClass)) ? self::$db->con->lastInsertId() : $id; $objId = empty($id = $this->mapper::getObjectId($obj, $refClass)) ? $this->db->con->lastInsertId() : $id;
\settype($objId, $this->mapper::COLUMNS[$this->mapper::PRIMARYFIELD]['type']); \settype($objId, $this->mapper::COLUMNS[$this->mapper::PRIMARYFIELD]['type']);
return $objId; return $objId;
@ -313,7 +313,7 @@ class WriteMapper extends DataMapperAbstract
return; return;
} }
$relQuery = new Builder(self::$db); $relQuery = new Builder($this->db);
$relQuery->into($this->mapper::HAS_MANY[$propertyName]['table']) $relQuery->into($this->mapper::HAS_MANY[$propertyName]['table'])
->insert($this->mapper::HAS_MANY[$propertyName]['external'], $this->mapper::HAS_MANY[$propertyName]['self']); ->insert($this->mapper::HAS_MANY[$propertyName]['external'], $this->mapper::HAS_MANY[$propertyName]['self']);
@ -331,7 +331,7 @@ class WriteMapper extends DataMapperAbstract
} }
try { try {
$sth = self::$db->con->prepare($relQuery->toSql()); $sth = $this->db->con->prepare($relQuery->toSql());
if ($sth !== false) { if ($sth !== false) {
$sth->execute(); $sth->execute();
} }

View File

@ -690,7 +690,7 @@ class Builder extends BuilderAbstract
*/ */
public function newest(string $column) : self public function newest(string $column) : self
{ {
$this->orderBy($column, 'DESC'); $this->orderBy($column, OrderType::DESC);
return $this; return $this;
} }
@ -706,7 +706,7 @@ class Builder extends BuilderAbstract
*/ */
public function oldest(string $column) : self public function oldest(string $column) : self
{ {
$this->orderBy($column, 'ASC'); $this->orderBy($column, OrderType::ASC);
return $this; return $this;
} }
@ -721,7 +721,7 @@ class Builder extends BuilderAbstract
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function orderBy(string | array $columns, string | array $order = 'DESC') : self public function orderBy(string | array $columns, string | array $order = OrderType::DESC) : self
{ {
if (\is_string($columns)) { if (\is_string($columns)) {
$columns = [$columns]; $columns = [$columns];