mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 01:38:41 +00:00
new datamapper mostly implemented
This commit is contained in:
parent
b44aad18d1
commit
aa004569f5
File diff suppressed because it is too large
Load Diff
|
|
@ -15,6 +15,7 @@ declare(strict_types=1);
|
|||
namespace phpOMS\DataStorage\Database\Mapper;
|
||||
|
||||
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
|
||||
use phpOMS\DataStorage\Database\Query\Builder;
|
||||
use phpOMS\DataStorage\Database\Query\OrderType;
|
||||
|
||||
/**
|
||||
|
|
@ -41,6 +42,8 @@ abstract class DataMapperAbstract
|
|||
|
||||
protected array $where = [];
|
||||
|
||||
protected ?Builder $query = null;
|
||||
|
||||
/**
|
||||
* Database connection.
|
||||
*
|
||||
|
|
@ -55,6 +58,13 @@ abstract class DataMapperAbstract
|
|||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function query(Builder $query = null) : self
|
||||
{
|
||||
$this->query = $query;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Only for relations, no impact on anything else
|
||||
public function with(string $member) : self
|
||||
{
|
||||
|
|
@ -191,5 +201,5 @@ abstract class DataMapperAbstract
|
|||
return $value;
|
||||
}
|
||||
|
||||
abstract public function execute(array ...$options) : mixed;
|
||||
abstract public function execute(...$options) : mixed;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ class DataMapperFactory
|
|||
* @var ConnectionAbstract
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static ConnectionAbstract $db;
|
||||
protected static ConnectionAbstract $db;
|
||||
|
||||
/**
|
||||
* Initialized objects for cross reference to reduce initialization costs
|
||||
|
|
@ -230,93 +230,6 @@ class DataMapperFactory
|
|||
return (new DeleteMapper(new static(), $db ?? self::$db))->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add initialized object to local cache
|
||||
*
|
||||
* @param string $mapper Mapper name
|
||||
* @param mixed $id Object id
|
||||
* @param object $obj Model to cache locally
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function addInitialized(string $mapper, mixed $id, object $obj = null) : void
|
||||
{
|
||||
if (!isset(self::$initObjects[$mapper])) {
|
||||
self::$initObjects[$mapper] = [];
|
||||
}
|
||||
|
||||
self::$initObjects[$mapper][$id] = [
|
||||
'obj' => $obj,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a object is initialized
|
||||
*
|
||||
* @param string $mapper Mapper name
|
||||
* @param mixed $id Object id
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function isInitialized(string $mapper, mixed $id) : bool
|
||||
{
|
||||
return !empty($id)
|
||||
&& isset(self::$initObjects[$mapper], self::$initObjects[$mapper][$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear cache
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function clearCache() : void
|
||||
{
|
||||
self::$initObjects = [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get initialized object
|
||||
*
|
||||
* @param string $mapper Mapper name
|
||||
* @param mixed $id Object id
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function getInitialized(string $mapper, mixed $id) : mixed
|
||||
{
|
||||
if (!self::isInitialized($mapper, $id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::$initObjects[$mapper][$id]['obj'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove initialized object
|
||||
*
|
||||
* @param string $mapper Mapper name
|
||||
* @param mixed $id Object id
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function removeInitialized(string $mapper, mixed $id) : void
|
||||
{
|
||||
if (isset(self::$initObjects[$mapper][$id])) {
|
||||
unset(self::$initObjects[$mapper][$id]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if object is null object
|
||||
*
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class DeleteMapper extends DataMapperAbstract
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function execute(array ...$options) : mixed
|
||||
public function execute(...$options) : mixed
|
||||
{
|
||||
switch($this->type) {
|
||||
case MapperType::DELETE:
|
||||
|
|
@ -59,126 +59,76 @@ class DeleteMapper extends DataMapperAbstract
|
|||
return null;
|
||||
}
|
||||
|
||||
$this->mapper::removeInitialized(static::class, $objId);
|
||||
$this->deleteSingleRelation($obj, $refClass, $this->mapper::BELONGS_TO);
|
||||
$this->deleteHasMany($refClass, $obj, $objId);
|
||||
$this->deleteModel($obj, $objId, $refClass);
|
||||
$this->deleteModel($objId);
|
||||
$this->deleteSingleRelation($obj, $refClass, $this->mapper::OWNS_ONE);
|
||||
|
||||
return $objId;
|
||||
}
|
||||
|
||||
private function deleteModel(object $obj, mixed $objId, \ReflectionClass $refClass = null) : void
|
||||
private function deleteModel(mixed $objId) : void
|
||||
{
|
||||
$query = new Builder($this->db);
|
||||
$query->delete()
|
||||
->from($this->mapper::TABLE)
|
||||
->where($this->mapper::TABLE . '.' . $this->mapper::PRIMARYFIELD, '=', $objId);
|
||||
|
||||
$refClass = $refClass ?? new \ReflectionClass($obj);
|
||||
$properties = $refClass->getProperties();
|
||||
|
||||
foreach ($properties as $property) {
|
||||
$propertyName = $property->getName();
|
||||
|
||||
if (isset($this->mapper::HAS_MANY[$propertyName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!($isPublic = $property->isPublic())) {
|
||||
$property->setAccessible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Orange-Management/phpOMS#233
|
||||
* On delete the relations and relation tables need to be deleted first
|
||||
* The exception is of course the belongsTo relation.
|
||||
*/
|
||||
foreach ($this->mapper::COLUMNS as $key => $column) {
|
||||
$value = $isPublic ? $obj->{$propertyName} : $property->getValue($obj);
|
||||
if (isset($this->mapper::OWNS_ONE[$propertyName])
|
||||
&& $column['internal'] === $propertyName
|
||||
) {
|
||||
$this->deleteOwnsOne($propertyName, $value);
|
||||
break;
|
||||
} elseif (isset($this->mapper::BELONGS_TO[$propertyName])
|
||||
&& $column['internal'] === $propertyName
|
||||
) {
|
||||
$this->deleteBelongsTo($propertyName, $value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isPublic) {
|
||||
$property->setAccessible(false);
|
||||
}
|
||||
}
|
||||
|
||||
$sth = $this->db->con->prepare($query->toSql());
|
||||
if ($sth !== false) {
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
|
||||
private function deleteBelongsTo(string $propertyName, mixed $obj) : mixed
|
||||
private function deleteSingleRelation(mixed $obj, \ReflectionClass $refClass, array $relation) : void
|
||||
{
|
||||
if (!\is_object($obj)) {
|
||||
return $obj;
|
||||
if (empty($relation)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $this->mapper::BELONGS_TO[$propertyName]['mapper'];
|
||||
foreach ($relation as $member => $relData) {
|
||||
if (!isset($this->with[$member])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var self $relMapper */
|
||||
$relMapper = $this->createRelationMapper($mapper::delete(db: $this->db), $propertyName);
|
||||
$relMapper->depth = $this->depth + 1;
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $relData['mapper'];
|
||||
|
||||
return $relMapper->execute($obj);
|
||||
}
|
||||
/** @var self $relMapper */
|
||||
$relMapper = $this->createRelationMapper($mapper::delete(db: $this->db), $member);
|
||||
$relMapper->depth = $this->depth + 1;
|
||||
|
||||
private function deleteOwnsOne(string $propertyName, mixed $obj) : mixed
|
||||
{
|
||||
if (!\is_object($obj)) {
|
||||
return $obj;
|
||||
$refProp = $refClass->getProperty($member);
|
||||
if (!$refProp->isPublic()) {
|
||||
$refProp->setAccessible(true);
|
||||
$relMapper->execute($refProp->getValue($obj));
|
||||
$refProp->setAccessible(false);
|
||||
} else {
|
||||
$relMapper->execute($obj->{$member});
|
||||
}
|
||||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $this->mapper::OWNS_ONE[$propertyName]['mapper'];
|
||||
|
||||
/**
|
||||
* @todo Orange-Management/phpOMS#??? [p:low] [t:question] [d:expert]
|
||||
* Deleting a owned one object is not recommended since it can be owned by something else?
|
||||
* Or does owns one mean that nothing else can have a relation to this model?
|
||||
*/
|
||||
|
||||
/** @var self $relMapper */
|
||||
$relMapper = $this->createRelationMapper($mapper::delete(db: $this->db), $propertyName);
|
||||
$relMapper->depth = $this->depth + 1;
|
||||
|
||||
return $relMapper->execute($obj);
|
||||
}
|
||||
|
||||
private function deleteHasMany(\ReflectionClass $refClass, object $obj, mixed $objId) : void
|
||||
{
|
||||
if (empty($this->with) || empty($this->mapper::HAS_MANY)) {
|
||||
if (empty($this->mapper::HAS_MANY)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->mapper::HAS_MANY as $propertyName => $rel) {
|
||||
if (!isset($this->mapper::HAS_MANY[$propertyName]['mapper'])) {
|
||||
throw new InvalidMapperException();
|
||||
}
|
||||
|
||||
if (isset($rel['column']) || !isset($this->with[$propertyName])) {
|
||||
foreach ($this->mapper::HAS_MANY as $member => $rel) {
|
||||
// always
|
||||
if (!isset($this->with[$member]) && !isset($rel['external'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$property = $refClass->getProperty($propertyName);
|
||||
|
||||
if (!($isPublic = $property->isPublic())) {
|
||||
$property->setAccessible(true);
|
||||
$values = $property->getValue($obj);
|
||||
$property->setAccessible(false);
|
||||
$objIds = [];
|
||||
$refProp = $refClass->getProperty($member);
|
||||
if (!$refProp->isPublic()) {
|
||||
$refProp->setAccessible(true);
|
||||
$values = $refProp->getValue($obj);
|
||||
$refProp->setAccessible(false);
|
||||
} else {
|
||||
$values = $obj->{$propertyName};
|
||||
$values = $obj->{$member};
|
||||
}
|
||||
|
||||
if (!\is_array($values)) {
|
||||
|
|
@ -187,70 +137,55 @@ class DeleteMapper extends DataMapperAbstract
|
|||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $this->mapper::HAS_MANY[$propertyName]['mapper'];
|
||||
$objsIds = [];
|
||||
$mapper = $this->mapper::HAS_MANY[$member]['mapper'];
|
||||
$relReflectionClass = !empty($values) ? new \ReflectionClass(\reset($values)) : null;
|
||||
|
||||
foreach ($values as $key => &$value) {
|
||||
foreach ($values as $key => $value) {
|
||||
if (!\is_object($value)) {
|
||||
// Is scalar => already in database
|
||||
$objsIds[$key] = $value;
|
||||
$objIds[$key] = $value;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$primaryKey = $mapper::getObjectId($value, $relReflectionClass);
|
||||
|
||||
// already in db
|
||||
if (!empty($primaryKey)) {
|
||||
$objsIds[$key] = $mapper::delete(db: $this->db)->execute($value);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Orange-Management/phpOMS#233
|
||||
* On delete the relations and relation tables need to be deleted first
|
||||
* The exception is of course the belongsTo relation.
|
||||
*/
|
||||
$objIds[$key] = $mapper::getObjectId($value, $relReflectionClass);
|
||||
}
|
||||
|
||||
$this->deleteRelationTable($propertyName, $objsIds, $objId);
|
||||
// delete relation tables
|
||||
if (isset($rel['external'])) {
|
||||
$this->deleteRelationTable($member, $objIds, $objId);
|
||||
} else {
|
||||
// only delete related obj if it is NOT in a relation table
|
||||
// if it is not in a relation table it must be directly related
|
||||
// this means it CAN ONLY be related to this object and not others
|
||||
foreach ($objIds as $id) {
|
||||
$mapper::delete(db: $this->db)->execute($id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteRelation(string $member, mixed $id1, mixed $id2) : bool
|
||||
public function deleteRelationTable(string $member, array $objIds = null, mixed $objId) : void
|
||||
{
|
||||
if (!isset($this->mapper::HAS_MANY[$member]) || !isset($this->mapper::HAS_MANY[$member]['external'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->mapper::removeInitialized(static::class, $id1);
|
||||
$this->deleteRelationTable($member, \is_array($id2) ? $id2 : [$id2], $id1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteRelationTable(string $propertyName, array $objsIds, mixed $objId) : void
|
||||
{
|
||||
if (empty($objsIds)
|
||||
|| $this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::TABLE
|
||||
|| $this->mapper::HAS_MANY[$propertyName]['table'] === $this->mapper::HAS_MANY[$propertyName]['mapper']::TABLE
|
||||
if ((empty($objIds) && $objIds !== null)
|
||||
|| $this->mapper::HAS_MANY[$member]['table'] === $this->mapper::TABLE
|
||||
|| $this->mapper::HAS_MANY[$member]['table'] === $this->mapper::HAS_MANY[$member]['mapper']::TABLE
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($objsIds as $src) {
|
||||
$relQuery = new Builder($this->db);
|
||||
$relQuery->delete()
|
||||
->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]['self'], '=', $objId, 'and');
|
||||
$relQuery = new Builder($this->db);
|
||||
$relQuery->delete()
|
||||
->from($this->mapper::HAS_MANY[$member]['table'])
|
||||
->where($this->mapper::HAS_MANY[$member]['table'] . '.' . $this->mapper::HAS_MANY[$member]['self'], '=', $objId);
|
||||
|
||||
$sth = $this->db->con->prepare($relQuery->toSql());
|
||||
if ($sth !== false) {
|
||||
$sth->execute();
|
||||
}
|
||||
if ($objIds !== null) {
|
||||
$relQuery->where($this->mapper::HAS_MANY[$member]['table'] . '.' . $this->mapper::HAS_MANY[$member]['external'], 'in', $objIds);
|
||||
}
|
||||
|
||||
$sth = $this->db->con->prepare($relQuery->toSql());
|
||||
if ($sth !== false) {
|
||||
$sth->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,7 +78,7 @@ class ReadMapper extends DataMapperAbstract
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function execute(array ...$options) : mixed
|
||||
public function execute(...$options) : mixed
|
||||
{
|
||||
switch($this->type) {
|
||||
case MapperType::GET:
|
||||
|
|
@ -116,15 +116,6 @@ class ReadMapper extends DataMapperAbstract
|
|||
|
||||
// Get initialized objects from memory cache.
|
||||
$obj = [];
|
||||
foreach ($primaryKeys as $index => $value) {
|
||||
if (!$this->mapper::isInitialized($this->mapper::class, $value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$obj[$value] = $this->mapper::getInitialized($this->mapper::class, $value);
|
||||
unset($this->where[$memberOfPrimaryField]);
|
||||
unset($primaryKeys[$index]);
|
||||
}
|
||||
|
||||
// Get remaining objects (not available in memory cache) or remaining where clauses.
|
||||
if (!empty($primaryKeys) || (!empty($this->where) || $emptyWhere)) {
|
||||
|
|
@ -133,7 +124,6 @@ class ReadMapper extends DataMapperAbstract
|
|||
foreach ($dbData as $row) {
|
||||
$value = $row[$this->mapper::PRIMARYFIELD . '_d' . $this->depth];
|
||||
$obj[$value] = $this->mapper::createBaseModel();
|
||||
$this->mapper::addInitialized($this->mapper::class, $value, $obj[$value]);
|
||||
|
||||
$obj[$value] = $this->populateAbstract($row, $obj[$value]);
|
||||
$this->loadHasManyRelations($obj[$value]);
|
||||
|
|
@ -153,7 +143,7 @@ class ReadMapper extends DataMapperAbstract
|
|||
|
||||
public function executeGetRaw(Builder $query = null) : array
|
||||
{
|
||||
$query ??= $this->getQuery($query);
|
||||
$query ??= $this->getQuery();
|
||||
|
||||
try {
|
||||
$results = false;
|
||||
|
|
@ -192,8 +182,7 @@ class ReadMapper extends DataMapperAbstract
|
|||
*/
|
||||
public function executeCount() : int
|
||||
{
|
||||
$query = $this->getQuery();
|
||||
$query->select('COUNT(*)');
|
||||
$query = $this->getQuery(null, ['COUNT(*)' => 'count']);
|
||||
|
||||
return (int) $query->execute()->fetchColumn();
|
||||
}
|
||||
|
|
@ -225,14 +214,18 @@ class ReadMapper extends DataMapperAbstract
|
|||
*/
|
||||
public function getQuery(Builder $query = null, array $columns = []) : Builder
|
||||
{
|
||||
$query ??= new Builder($this->db);
|
||||
$query ??= $this->query ?? new Builder($this->db, true);
|
||||
$columns = empty($columns)
|
||||
? (empty($this->columns) ? $this->mapper::COLUMNS : $this->columns)
|
||||
: $columns;
|
||||
|
||||
foreach ($columns as $key => $values) {
|
||||
if ($values['writeonly'] ?? false === false) {
|
||||
$query->selectAs($this->mapper::TABLE . '_d' . $this->depth . '.' . $key, $key . '_d' . $this->depth);
|
||||
if (\is_string($values)) {
|
||||
$query->selectAs($key, $values);
|
||||
} else {
|
||||
if (($values['writeonly'] ?? false) === false) {
|
||||
$query->selectAs($this->mapper::TABLE . '_d' . $this->depth . '.' . $key, $key . '_d' . $this->depth);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -325,7 +318,7 @@ class ReadMapper extends DataMapperAbstract
|
|||
) {
|
||||
$rel = $this->mapper::OWNS_ONE[$member] ?? ($this->mapper::BELONGS_TO[$member] ?? ($this->mapper::HAS_MANY[$member] ?? null));
|
||||
} else {
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($data as $index => $with) {
|
||||
|
|
@ -644,11 +637,6 @@ class ReadMapper extends DataMapperAbstract
|
|||
return $mapper::createNullModel();
|
||||
}
|
||||
|
||||
$obj = $mapper::getInitialized($mapper, $result[$mapper::PRIMARYFIELD . '_d' . ($this->depth + 1)]);
|
||||
if ($obj !== null) {
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $ownsOneMapper */
|
||||
$ownsOneMapper = $this->createRelationMapper($mapper::get($this->db), $member);
|
||||
$ownsOneMapper->depth = $this->depth + 1;
|
||||
|
|
@ -703,16 +691,11 @@ class ReadMapper extends DataMapperAbstract
|
|||
/** @var class-string<DataMapperFactory> $belongsToMapper */
|
||||
$belongsToMapper = $this->createRelationMapper($mapper::get($this->db), $member);
|
||||
$belongsToMapper->depth = $this->depth + 1;
|
||||
$belongsToMapper->where($this->mapper::BELONGS_TO[$member]['by'], $result[$mapper::getColumnByMember($this->mapper::BELONGS_TO[$member]['by']) . '_d' . $this->depth], '=');
|
||||
$belongsToMapper->where($this->mapper::BELONGS_TO[$member]['by'], $result[$mapper::getColumnByMember($this->mapper::BELONGS_TO[$member]['by']) . '_d' . $this->depth + 1], '=');
|
||||
|
||||
return $belongsToMapper->execute();
|
||||
}
|
||||
|
||||
$obj = $mapper::getInitialized($mapper, $result[$mapper::PRIMARYFIELD . '_d' . ($this->depth + 1)]);
|
||||
if ($obj !== null) {
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $belongsToMapper */
|
||||
$belongsToMapper = $this->createRelationMapper($mapper::get($this->db), $member);
|
||||
$belongsToMapper->depth = $this->depth + 1;
|
||||
|
|
@ -731,7 +714,7 @@ class ReadMapper extends DataMapperAbstract
|
|||
*/
|
||||
public function loadHasManyRelations(mixed $obj) : void
|
||||
{
|
||||
if (empty($this->with) || empty($this->mapper::HAS_MANY)) {
|
||||
if (empty($this->with)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -740,52 +723,91 @@ class ReadMapper extends DataMapperAbstract
|
|||
return;
|
||||
}
|
||||
|
||||
$refClass = new \ReflectionClass($obj);
|
||||
$refClass = null;
|
||||
|
||||
// @todo: check if there are more cases where the relation is already loaded with joins etc.
|
||||
// there can be pseudo has many elements like localizations. They are has manies but these are already loaded with joins!
|
||||
foreach ($this->mapper::HAS_MANY as $member => $many) {
|
||||
if (isset($many['column']) || !isset($this->with[$member])) {
|
||||
foreach ($this->with as $member => $withData) {
|
||||
if (isset($this->mapper::HAS_MANY[$member])) {
|
||||
$many = $this->mapper::HAS_MANY[$member];
|
||||
if (isset($many['column'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$query = new Builder($this->db, true);
|
||||
$src = $many['external'] ?? $many['mapper']::PRIMARYFIELD;
|
||||
|
||||
// @todo: what if a specific column name is defined instead of primaryField for the join? Fix, it should be stored in 'column'
|
||||
$query->select($many['table'] . '.' . $src)
|
||||
->from($many['table'])
|
||||
->where($many['table'] . '.' . $many['self'], '=', $primaryKey);
|
||||
|
||||
if ($many['mapper']::TABLE !== $many['table']) {
|
||||
$query->leftJoin($many['mapper']::TABLE)
|
||||
->on($many['table'] . '.' . $src, '=', $many['mapper']::TABLE . '.' . $many['mapper']::PRIMARYFIELD);
|
||||
}
|
||||
|
||||
$sth = $this->db->con->prepare($query->toSql());
|
||||
if ($sth === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sth->execute();
|
||||
$result = $sth->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
||||
if (empty($result)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$objects = $this->createRelationMapper($many['mapper']::get(db: $this->db), $member)
|
||||
->where($many['mapper']::COLUMNS[$many['mapper']::PRIMARYFIELD]['internal'], $result, 'in')
|
||||
->execute();
|
||||
|
||||
if ($refClass === null) {
|
||||
$refClass = new \ReflectionClass($obj);
|
||||
}
|
||||
|
||||
$refProp = $refClass->getProperty($member);
|
||||
if (!$refProp->isPublic()) {
|
||||
$refProp->setAccessible(true);
|
||||
$refProp->setValue($obj, !\is_array($objects)
|
||||
? [$many['mapper']::getObjectId($objects) => $objects]
|
||||
: $objects
|
||||
);
|
||||
$refProp->setAccessible(false);
|
||||
} else {
|
||||
$obj->{$member} = !\is_array($objects)
|
||||
? [$many['mapper']::getObjectId($objects) => $objects]
|
||||
: $objects;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
} elseif (isset($this->mapper::OWNS_ONE[$member])
|
||||
|| isset($this->mapper::BELONGS_TO[$member])
|
||||
) {
|
||||
$relation = isset($this->mapper::OWNS_ONE[$member])
|
||||
? $this->mapper::OWNS_ONE[$member]
|
||||
: $this->mapper::BELONGS_TO[$member];
|
||||
|
||||
$query = new Builder($this->db);
|
||||
$src = $many['external'] ?? $many['mapper']::PRIMARYFIELD;
|
||||
if (\count($withData) < 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// @todo: what if a specific column name is defined instead of primaryField for the join? Fix, it should be stored in 'column'
|
||||
$query->select($many['table'] . '.' . $src)
|
||||
->from($many['table'])
|
||||
->where($many['table'] . '.' . $many['self'], '=', $primaryKey);
|
||||
if ($refClass === null) {
|
||||
$refClass = new \ReflectionClass($obj);
|
||||
}
|
||||
|
||||
if ($many['mapper']::TABLE !== $many['table']) {
|
||||
$query->leftJoin($many['mapper']::TABLE)
|
||||
->on($many['table'] . '.' . $src, '=', $many['mapper']::TABLE . '.' . $many['mapper']::PRIMARYFIELD);
|
||||
}
|
||||
/** @var ReadMapper $relMapper */
|
||||
$relMapper = $this->createRelationMapper($relation['mapper']::reader($this->db), $member);
|
||||
|
||||
$sth = $this->db->con->prepare($query->toSql());
|
||||
if ($sth === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sth->execute();
|
||||
$result = $sth->fetchAll(\PDO::FETCH_COLUMN);
|
||||
|
||||
$objects = $this->createRelationMapper($many['mapper']::get(db: $this->db), $member)
|
||||
->where($many['mapper']::COLUMNS[$many['mapper']::PRIMARYFIELD]['internal'], $result, 'in')
|
||||
->execute();
|
||||
|
||||
$refProp = $refClass->getProperty($member);
|
||||
if (!$refProp->isPublic()) {
|
||||
$refProp->setAccessible(true);
|
||||
$refProp->setValue($obj, !\is_array($objects) && !isset($this->mapper::HAS_MANY[$member]['conditional'])
|
||||
? [$many['mapper']::getObjectId($objects) => $objects]
|
||||
: $objects
|
||||
);
|
||||
$refProp->setAccessible(false);
|
||||
} else {
|
||||
$obj->{$member} = !\is_array($objects) && !isset($this->mapper::HAS_MANY[$member]['conditional'])
|
||||
? [$many['mapper']::getObjectId($objects) => $objects]
|
||||
: $objects;
|
||||
$refProp = $refClass->getProperty($member);
|
||||
if (!$refProp->isPublic()) {
|
||||
$refProp->setAccessible(true);
|
||||
$relMapper->loadHasManyRelations($refProp->getValue($obj));
|
||||
$refProp->setAccessible(false);
|
||||
} else {
|
||||
$relMapper->loadHasManyRelations($obj->{$member});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class UpdateMapper extends DataMapperAbstract
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function execute(array ...$options) : mixed
|
||||
public function execute(...$options) : mixed
|
||||
{
|
||||
switch($this->type) {
|
||||
case MapperType::UPDATE:
|
||||
|
|
@ -61,8 +61,6 @@ class UpdateMapper extends DataMapperAbstract
|
|||
return $objId === 0 ? null : $objId;
|
||||
}
|
||||
|
||||
$this->mapper::addInitialized(static::class, $objId, $obj);
|
||||
|
||||
$this->updateHasMany($refClass, $obj, $objId);
|
||||
|
||||
if (empty($objId)) {
|
||||
|
|
@ -184,6 +182,7 @@ class UpdateMapper extends DataMapperAbstract
|
|||
|
||||
private function updateHasMany(\ReflectionClass $refClass, object $obj, mixed $objId) : void
|
||||
{
|
||||
// @todo: what if has_one has a has_many child (see readmapper, we already solved this here)
|
||||
if (empty($this->with) || empty($this->mapper::HAS_MANY)) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ class WriteMapper extends DataMapperAbstract
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function execute(array ...$options) : mixed
|
||||
public function execute(...$options) : mixed
|
||||
{
|
||||
switch($this->type) {
|
||||
case MapperType::CREATE:
|
||||
|
|
@ -181,16 +181,12 @@ class WriteMapper extends DataMapperAbstract
|
|||
} else {
|
||||
$obj = $obj->{$this->mapper::BELONGS_TO[$propertyName]['by']};
|
||||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $this->mapper::BELONGS_TO[$propertyName]['mapper']::getBelongsTo($this->mapper::BELONGS_TO[$propertyName]['by'])['mapper'];
|
||||
$primaryKey = $mapper::getObjectId($obj);
|
||||
} else {
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $this->mapper::BELONGS_TO[$propertyName]['mapper'];
|
||||
$primaryKey = $mapper::getObjectId($obj);
|
||||
}
|
||||
|
||||
/** @var class-string<DataMapperFactory> $mapper */
|
||||
$mapper = $this->mapper::BELONGS_TO[$propertyName]['mapper'];
|
||||
$primaryKey = $mapper::getObjectId($obj);
|
||||
|
||||
// @todo: the $mapper::create() might cause a problem is 'by' is set. because we don't want to create this obj but the child obj.
|
||||
return empty($primaryKey) ? $mapper::create(db: $this->db)->execute($obj) : $primaryKey;
|
||||
}
|
||||
|
|
@ -241,7 +237,7 @@ class WriteMapper extends DataMapperAbstract
|
|||
$property->setAccessible(false);
|
||||
}
|
||||
|
||||
// conditionals
|
||||
// @todo: conditionals???
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -274,28 +270,28 @@ class WriteMapper extends DataMapperAbstract
|
|||
if (!isset($this->mapper::HAS_MANY[$propertyName]['external'])) {
|
||||
$relProperty = $relReflectionClass->getProperty($internalName);
|
||||
|
||||
if (!$isPublic) {
|
||||
if (!($isRelPublic = $relProperty->isPublic())) {
|
||||
$relProperty->setAccessible(true);
|
||||
}
|
||||
|
||||
// todo maybe consider to just set the column type to object, and then check for that (might be faster)
|
||||
if (isset($mapper::$belongsTo[$internalName])
|
||||
|| isset($mapper::$ownsOne[$internalName])
|
||||
if (isset($mapper::BELONGS_TO[$internalName])
|
||||
|| isset($mapper::OWNS_ONE[$internalName])
|
||||
) {
|
||||
if (!$isPublic) {
|
||||
if (!$isRelPublic) {
|
||||
$relProperty->setValue($value, $this->mapper::createNullModel($objId));
|
||||
} else {
|
||||
$value->{$internalName} = $this->mapper::createNullModel($objId);
|
||||
}
|
||||
} else {
|
||||
if (!$isPublic) {
|
||||
if (!$isRelPublic) {
|
||||
$relProperty->setValue($value, $objId);
|
||||
} else {
|
||||
$value->{$internalName} = $objId;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$isPublic) {
|
||||
if (!$isRelPublic) {
|
||||
$relProperty->setAccessible(false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Localization\Defaults;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
/**
|
||||
* Mapper class.
|
||||
|
|
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
|
|||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class CityMapper extends DataMapperAbstract
|
||||
class CityMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -32,7 +32,7 @@ class CityMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'city_id' => ['name' => 'city_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'city_city' => ['name' => 'city_city', 'type' => 'string', 'internal' => 'name'],
|
||||
'city_country' => ['name' => 'city_country', 'type' => 'string', 'internal' => 'countryCode'],
|
||||
|
|
@ -48,7 +48,7 @@ class CityMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $table = 'city';
|
||||
public const TABLE = 'city';
|
||||
|
||||
/**
|
||||
* Primary field name.
|
||||
|
|
@ -56,5 +56,5 @@ class CityMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $primaryField = 'city_id';
|
||||
public const PRIMARYFIELD ='city_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Localization\Defaults;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
/**
|
||||
* Mapper class.
|
||||
|
|
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
|
|||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class CountryMapper extends DataMapperAbstract
|
||||
class CountryMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -32,7 +32,7 @@ class CountryMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'country_id' => ['name' => 'country_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'country_name' => ['name' => 'country_name', 'type' => 'string', 'internal' => 'name'],
|
||||
'country_code2' => ['name' => 'country_code2', 'type' => 'string', 'internal' => 'code2'],
|
||||
|
|
@ -48,7 +48,7 @@ class CountryMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $table = 'country';
|
||||
public const TABLE = 'country';
|
||||
|
||||
/**
|
||||
* Primary field name.
|
||||
|
|
@ -56,5 +56,5 @@ class CountryMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $primaryField = 'country_id';
|
||||
public const PRIMARYFIELD ='country_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Localization\Defaults;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
/**
|
||||
* Mapper class.
|
||||
|
|
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
|
|||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class CurrencyMapper extends DataMapperAbstract
|
||||
final class CurrencyMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -32,7 +32,7 @@ final class CurrencyMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'currency_id' => ['name' => 'currency_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'currency_name' => ['name' => 'currency_name', 'type' => 'string', 'internal' => 'name'],
|
||||
'currency_code' => ['name' => 'currency_code', 'type' => 'string', 'internal' => 'code'],
|
||||
|
|
@ -49,7 +49,7 @@ final class CurrencyMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $table = 'currency';
|
||||
public const TABLE = 'currency';
|
||||
|
||||
/**
|
||||
* Primary field name.
|
||||
|
|
@ -57,5 +57,5 @@ final class CurrencyMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $primaryField = 'currency_id';
|
||||
public const PRIMARYFIELD ='currency_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Localization\Defaults;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
/**
|
||||
* Mapper class.
|
||||
|
|
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
|
|||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class IbanMapper extends DataMapperAbstract
|
||||
class IbanMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -32,7 +32,7 @@ class IbanMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'iban_id' => ['name' => 'iban_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'iban_country' => ['name' => 'iban_country', 'type' => 'string', 'internal' => 'country'],
|
||||
'iban_chars' => ['name' => 'iban_chars', 'type' => 'int', 'internal' => 'chars'],
|
||||
|
|
@ -46,7 +46,7 @@ class IbanMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $table = 'iban';
|
||||
public const TABLE = 'iban';
|
||||
|
||||
/**
|
||||
* Primary field name.
|
||||
|
|
@ -54,5 +54,5 @@ class IbanMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $primaryField = 'iban_id';
|
||||
public const PRIMARYFIELD ='iban_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\Localization\Defaults;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
/**
|
||||
* Mapper class.
|
||||
|
|
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
|
|||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class LanguageMapper extends DataMapperAbstract
|
||||
class LanguageMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -32,7 +32,7 @@ class LanguageMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'language_id' => ['name' => 'language_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'language_name' => ['name' => 'language_name', 'type' => 'string', 'internal' => 'name'],
|
||||
'language_native' => ['name' => 'language_native', 'type' => 'string', 'internal' => 'native'],
|
||||
|
|
@ -47,7 +47,7 @@ class LanguageMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $table = 'language';
|
||||
public const TABLE = 'language';
|
||||
|
||||
/**
|
||||
* Primary field name.
|
||||
|
|
@ -55,5 +55,5 @@ class LanguageMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $primaryField = 'language_id';
|
||||
public const PRIMARYFIELD ='language_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,4 +24,8 @@ namespace phpOMS\Localization;
|
|||
*/
|
||||
final class NullLocalization extends Localization
|
||||
{
|
||||
public function __construct(int $id = 0)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ abstract class ModuleAbstract
|
|||
$id = 0;
|
||||
|
||||
if (\is_string($mapper)) {
|
||||
$id = $mapper::create($obj);
|
||||
$id = $mapper::create()->execute($obj);
|
||||
} else {
|
||||
$mapper();
|
||||
}
|
||||
|
|
@ -305,7 +305,7 @@ abstract class ModuleAbstract
|
|||
$id = 0;
|
||||
|
||||
if (\is_string($mapper)) {
|
||||
$id = $mapper::create($obj);
|
||||
$id = $mapper::create()->execute($obj);
|
||||
} else {
|
||||
$mapper();
|
||||
}
|
||||
|
|
@ -344,7 +344,7 @@ abstract class ModuleAbstract
|
|||
$id = 0;
|
||||
|
||||
if (\is_string($mapper)) {
|
||||
$id = $mapper::update($new);
|
||||
$id = $mapper::update()->execute($new);
|
||||
} else {
|
||||
$mapper();
|
||||
}
|
||||
|
|
@ -383,7 +383,7 @@ abstract class ModuleAbstract
|
|||
$id = 0;
|
||||
|
||||
if (\is_string($mapper)) {
|
||||
$id = $mapper::delete($obj);
|
||||
$id = $mapper::delete()->execute($obj);
|
||||
} else {
|
||||
$mapper();
|
||||
}
|
||||
|
|
@ -421,7 +421,7 @@ abstract class ModuleAbstract
|
|||
$trigger = static::NAME . '-' . $trigger . '-relation-create';
|
||||
|
||||
$this->app->eventManager->triggerSimilar('PRE:Module:' . $trigger, '', $rel1);
|
||||
$mapper::createRelation($field, $rel1, $rel2);
|
||||
$mapper::writer()->createRelationTable($field, \is_array($rel2) ? $rel2 : [$rel2], $rel1);
|
||||
$this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '',
|
||||
[
|
||||
$account,
|
||||
|
|
@ -455,7 +455,7 @@ abstract class ModuleAbstract
|
|||
$trigger = static::NAME . '-' . $trigger . '-relation-delete';
|
||||
|
||||
$this->app->eventManager->triggerSimilar('PRE:Module:' . $trigger, '', $rel1);
|
||||
$mapper::deleteRelation($field, $rel1, $rel2);
|
||||
$mapper::remover()->deleteRelationTable($field, \is_array($rel2) ? $rel2 : [$rel2], $rel1);
|
||||
$this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '',
|
||||
[
|
||||
$account,
|
||||
|
|
|
|||
|
|
@ -137,8 +137,8 @@ final class SocketRouter implements RouterInterface
|
|||
|
||||
foreach ($destination as $d) {
|
||||
// if permission check is invalid
|
||||
if ((isset($d['permission']) && $account === null)
|
||||
|| (isset($d['permission'])
|
||||
if ((isset($d['permission']) && !empty($d['permission']) && $account === null)
|
||||
|| (isset($d['permission']) && !empty($d['permission'])
|
||||
&& !$account?->hasPermission(
|
||||
$d['permission']['type'] ?? null, $orgId, $app, $d['permission']['module'] ?? null, $d['permission']['state'] ?? null
|
||||
)
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ if (\is_file('vendor/autoload.php')) {
|
|||
require_once __DIR__ . '/../Autoloader.php';
|
||||
|
||||
use phpOMS\DataStorage\Database\DatabasePool;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\DataStorage\Session\HttpSession;
|
||||
use phpOMS\Log\FileLogger;
|
||||
|
||||
|
|
@ -358,7 +358,7 @@ $GLOBALS['dbpool']->create('update', $CONFIG['db']['core']['masters']['update'])
|
|||
$GLOBALS['dbpool']->create('delete', $CONFIG['db']['core']['masters']['delete']);
|
||||
$GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']);
|
||||
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
|
||||
$GLOBALS['frameworkpath'] = '/';
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
declare(strict_types=1);
|
||||
namespace phpOMS\tests\DataStorage\Database;
|
||||
|
||||
use phpOMS\DataStorage\Database\Query\OrderType;
|
||||
use phpOMS\tests\DataStorage\Database\TestModel\BaseModel;
|
||||
use phpOMS\tests\DataStorage\Database\TestModel\BaseModelMapper;
|
||||
use phpOMS\tests\DataStorage\Database\TestModel\Conditional;
|
||||
|
|
@ -23,7 +24,7 @@ use phpOMS\tests\DataStorage\Database\TestModel\ManyToManyRelModelMapper;
|
|||
use phpOMS\tests\DataStorage\Database\TestModel\NullBaseModel;
|
||||
|
||||
/**
|
||||
* @testdox phpOMS\tests\DataStorage\Database\DataMapperAbstractTest: Datamapper for database models
|
||||
* @testdox phpOMS\tests\DataStorage\Database\Mapper\DataMapperAbstractTest: Datamapper for database models
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
|
|
@ -161,7 +162,6 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
protected function tearDown() : void
|
||||
{
|
||||
BaseModelMapper::clearCache();
|
||||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_conditional')->execute();
|
||||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_base')->execute();
|
||||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_belongs_to_one')->execute();
|
||||
|
|
@ -171,83 +171,70 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_has_many_rel_relations')->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper has the expected default values after initialization
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @group framework
|
||||
*/
|
||||
public function testDefault() : void
|
||||
{
|
||||
self::assertEquals('test_base_id', BaseModelMapper::getPrimaryField());
|
||||
self::assertEquals('test_base', BaseModelMapper::getTable());
|
||||
self::assertEquals('test_base_datetime', BaseModelMapper::getCreatedAt());
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully creates a database entry of a model
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\ReadMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\WriteMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\UpdateMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DeleteMapper
|
||||
* @group framework
|
||||
*/
|
||||
public function testCreate() : void
|
||||
{
|
||||
self::assertGreaterThan(0, BaseModelMapper::create($this->model));
|
||||
self::assertGreaterThan(0, BaseModelMapper::create()->execute($this->model));
|
||||
self::assertGreaterThan(0, $this->model->getId());
|
||||
}
|
||||
|
||||
public function testCreateNullModel() : void
|
||||
{
|
||||
$nullModel1 = new NullBaseModel();
|
||||
self::assertNull(BaseModelMapper::create($nullModel1));
|
||||
self::assertNull(BaseModelMapper::create()->execute($nullModel1));
|
||||
|
||||
$nullModel2 = new NullBaseModel(77);
|
||||
self::assertEquals(77, BaseModelMapper::create($nullModel2));
|
||||
self::assertEquals(77, BaseModelMapper::create()->execute($nullModel2));
|
||||
}
|
||||
|
||||
public function testCreateAlreadyCreatedModel() : void
|
||||
{
|
||||
self::assertGreaterThan(0, $id = BaseModelMapper::create($this->model));
|
||||
self::assertGreaterThan(0, $id = BaseModelMapper::create()->execute($this->model));
|
||||
self::assertGreaterThan(0, $this->model->getId());
|
||||
self::assertEquals($id, BaseModelMapper::create($this->model));
|
||||
self::assertEquals($id, BaseModelMapper::create()->execute($this->model));
|
||||
self::assertEquals($id, $this->model->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully creates a database entry of array data
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @group framework
|
||||
*/
|
||||
public function testCreateArray() : void
|
||||
{
|
||||
self::assertGreaterThan(0, BaseModelMapper::createArray($this->modelArray));
|
||||
self::assertGreaterThan(0, $this->modelArray['id']);
|
||||
}
|
||||
|
||||
public function testCreateHasManyRelation() : void
|
||||
{
|
||||
$id1 = BaseModelMapper::create($this->model);
|
||||
$id1 = BaseModelMapper::create()->execute($this->model);
|
||||
|
||||
$count1 = \count($this->model->hasManyRelations);
|
||||
|
||||
$hasMany = new ManyToManyRelModel();
|
||||
$id2 = ManyToManyRelModelMapper::create($hasMany);
|
||||
$id2 = ManyToManyRelModelMapper::create()->execute($hasMany);
|
||||
|
||||
BaseModelMapper::createRelation('hasManyRelations', $id1, $id2);
|
||||
BaseModelMapper::writer()->createRelationTable('hasManyRelations', [$id2], $id1);
|
||||
|
||||
BaseModelMapper::clearCache();
|
||||
|
||||
$base = BaseModelMapper::get($id1);
|
||||
$base = BaseModelMapper::get()->with('hasManyRelations')->where('id', $id1)->execute();
|
||||
self::assertCount($count1 + 1, $base->hasManyRelations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully returns a database entry as model
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\ReadMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\WriteMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\UpdateMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DeleteMapper
|
||||
* @group framework
|
||||
*/
|
||||
public function testRead() : void
|
||||
{
|
||||
$id = BaseModelMapper::create($this->model);
|
||||
$modelR = BaseModelMapper::get($id);
|
||||
$id = BaseModelMapper::create()->execute($this->model);
|
||||
|
||||
/** @var BaseModel $modelR */
|
||||
$modelR = BaseModelMapper::get()->where('id', $id)->execute();
|
||||
|
||||
self::assertEquals($this->model->getId(), $modelR->getId());
|
||||
self::assertEquals($this->model->string, $modelR->string);
|
||||
|
|
@ -275,14 +262,14 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public function testGetAll() : void
|
||||
{
|
||||
BaseModelMapper::create($this->model);
|
||||
self::assertCount(1, BaseModelMapper::getAll());
|
||||
BaseModelMapper::create()->execute($this->model);
|
||||
self::assertCount(1, BaseModelMapper::getAll()->execute());
|
||||
}
|
||||
|
||||
public function testGetFor() : void
|
||||
{
|
||||
$id = BaseModelMapper::create($this->model);
|
||||
$for = ManyToManyDirectModelMapper::getFor($id, 'to');
|
||||
$id = BaseModelMapper::create()->execute($this->model);
|
||||
$for = ManyToManyDirectModelMapper::getAll()->where('to', $id)->execute();
|
||||
|
||||
self::assertEquals(
|
||||
\reset($this->model->hasManyDirect)->string,
|
||||
|
|
@ -298,18 +285,22 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$model2 = new BaseModel();
|
||||
$model2->string = '456';
|
||||
|
||||
$id1 = BaseModelMapper::create($model1);
|
||||
$id2 = BaseModelMapper::create($model2);
|
||||
$id1 = BaseModelMapper::create()->execute($model1);
|
||||
$id2 = BaseModelMapper::create()->execute($model2);
|
||||
|
||||
$by = BaseModelMapper::getBy('456', 'string');
|
||||
$by = BaseModelMapper::get()->where('string', '456')->execute();
|
||||
self::assertEquals($model2->getId(), $by->getId());
|
||||
}
|
||||
|
||||
public function testGetCached() : void
|
||||
{
|
||||
$id = BaseModelMapper::create($this->model);
|
||||
$modelR = BaseModelMapper::get($id);
|
||||
$modelR2 = BaseModelMapper::get($id);
|
||||
$id = BaseModelMapper::create()->execute($this->model);
|
||||
|
||||
/** @var BaseModel $modelR */
|
||||
$modelR = BaseModelMapper::get()->where('id', $id)->execute();
|
||||
|
||||
/** @var BaseModel $modelR2 */
|
||||
$modelR2 = BaseModelMapper::get()->where('id', $id)->execute();
|
||||
|
||||
self::assertEquals($modelR->getId(), $modelR2->getId());
|
||||
}
|
||||
|
|
@ -318,23 +309,28 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
{
|
||||
$model1 = new BaseModel();
|
||||
$model1->datetime = new \DateTime('now');
|
||||
$id1 = BaseModelMapper::create($model1);
|
||||
$id1 = BaseModelMapper::create()->execute($model1);
|
||||
\sleep(1);
|
||||
$model2 = new BaseModel();
|
||||
$model2->datetime = new \DateTime('now');
|
||||
$id2 = BaseModelMapper::create($model2);
|
||||
$id2 = BaseModelMapper::create()->execute($model2);
|
||||
|
||||
$newest = BaseModelMapper::getNewest();
|
||||
$newest = BaseModelMapper::getAll()->sort('id', OrderType::DESC)->limit(1)->execute();
|
||||
self::assertEquals($id2, \reset($newest)->getId());
|
||||
}
|
||||
|
||||
public function testGetNullModel() : void
|
||||
{
|
||||
self::assertEquals(NullBaseModel::class, \get_class(BaseModelMapper::get(99)));
|
||||
self::assertEquals(NullBaseModel::class, \get_class(BaseModelMapper::get()->where('id', 99)->execute()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\ReadMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\WriteMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\UpdateMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DeleteMapper
|
||||
* @group framework
|
||||
*/
|
||||
public function testFind() : void
|
||||
|
|
@ -347,18 +343,23 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$model2->string = 'hallo sir';
|
||||
$model3->string = 'seasiren';
|
||||
|
||||
BaseModelMapper::create($model1);
|
||||
BaseModelMapper::create($model2);
|
||||
BaseModelMapper::create($model3);
|
||||
BaseModelMapper::create()->execute($model1);
|
||||
BaseModelMapper::create()->execute($model2);
|
||||
BaseModelMapper::create()->execute($model3);
|
||||
|
||||
$found = BaseModelMapper::find('sir');
|
||||
$found = BaseModelMapper::getAll()->where('string', '%sir%' , 'LIKE')->execute();
|
||||
self::assertCount(2, $found);
|
||||
self::assertEquals($model2->string, \reset($found)->string);
|
||||
self::assertEquals($model3->string, \end($found)->string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\ReadMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\WriteMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\UpdateMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DeleteMapper
|
||||
* @group framework
|
||||
*/
|
||||
public function testWithConditional() : void
|
||||
|
|
@ -371,29 +372,29 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$model2->string = 'hallo sir';
|
||||
$model3->string = 'seasiren';
|
||||
|
||||
$id1 = BaseModelMapper::create($model1);
|
||||
$id2 = BaseModelMapper::create($model2);
|
||||
$id3 = BaseModelMapper::create($model3);
|
||||
$id1 = BaseModelMapper::create()->execute($model1);
|
||||
$id2 = BaseModelMapper::create()->execute($model2);
|
||||
$id3 = BaseModelMapper::create()->execute($model3);
|
||||
|
||||
$cond1 = new Conditional();
|
||||
$cond1->language = 'de';
|
||||
$cond1->title = 'cond1_de';
|
||||
$cond1->base = $id1;
|
||||
ConditionalMapper::create($cond1);
|
||||
ConditionalMapper::create()->execute($cond1);
|
||||
|
||||
$cond2 = new Conditional();
|
||||
$cond2->language = 'en';
|
||||
$cond2->title = 'cond1_en';
|
||||
$cond2->base = $id1;
|
||||
ConditionalMapper::create($cond2);
|
||||
ConditionalMapper::create()->execute($cond2);
|
||||
|
||||
$cond3 = new Conditional();
|
||||
$cond3->language = 'de';
|
||||
$cond3->title = 'cond2_de';
|
||||
$cond3->base = $id2;
|
||||
ConditionalMapper::create($cond3);
|
||||
ConditionalMapper::create()->execute($cond3);
|
||||
|
||||
$found = BaseModelMapper::with('language', 'de')::getAll();
|
||||
$found = BaseModelMapper::getAll()->with('conditional')->where('conditional/language', 'de')->execute();
|
||||
self::assertCount(2, $found);
|
||||
self::assertEquals($model1->string, \reset($found)->string);
|
||||
self::assertEquals($model2->string, \end($found)->string);
|
||||
|
|
@ -401,50 +402,20 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
self::assertEquals('cond2_de', \end($found)->conditional);
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully returns a database entry as array
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @group framework
|
||||
*/
|
||||
public function testReadArray() : void
|
||||
{
|
||||
$id = BaseModelMapper::createArray($this->modelArray);
|
||||
$modelR = BaseModelMapper::getArray($id);
|
||||
|
||||
self::assertEquals($this->modelArray['id'], $modelR['id']);
|
||||
self::assertEquals($this->modelArray['string'], $modelR['string']);
|
||||
self::assertEquals($this->modelArray['int'], $modelR['int']);
|
||||
self::assertEquals($this->modelArray['bool'], $modelR['bool']);
|
||||
self::assertEquals($this->modelArray['float'], $modelR['float']);
|
||||
self::assertEquals($this->modelArray['null'], $modelR['null']);
|
||||
self::assertEquals($this->modelArray['datetime']->format('Y-m-d'), $modelR['datetime']->format('Y-m-d'));
|
||||
self::assertNull($modelR['datetime_null']);
|
||||
|
||||
self::assertCount(2, $modelR['hasManyDirect']);
|
||||
self::assertCount(2, $modelR['hasManyRelations']);
|
||||
self::assertEquals(\reset($this->modelArray['hasManyDirect'])['string'], \reset($modelR['hasManyDirect'])['string']);
|
||||
self::assertEquals(\reset($this->modelArray['hasManyRelations'])['string'], \reset($modelR['hasManyRelations'])['string']);
|
||||
self::assertEquals($this->modelArray['ownsOneSelf']['string'], $modelR['ownsOneSelf']['string']);
|
||||
self::assertEquals($this->modelArray['belongsToOne']['string'], $modelR['belongsToOne']['string']);
|
||||
|
||||
$for = ManyToManyDirectModelMapper::getForArray($id, 'to');
|
||||
self::assertEquals(
|
||||
\reset($this->modelArray['hasManyDirect'])['string'],
|
||||
\reset($for)['string']
|
||||
);
|
||||
|
||||
self::assertCount(1, BaseModelMapper::getAllArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully updates a database entry from a model
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\ReadMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\WriteMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\UpdateMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DeleteMapper
|
||||
* @group framework
|
||||
*/
|
||||
public function testUpdate() : void
|
||||
{
|
||||
$id = BaseModelMapper::create($this->model);
|
||||
$modelR = BaseModelMapper::get($id);
|
||||
$id = BaseModelMapper::create()->execute($this->model);
|
||||
$modelR = BaseModelMapper::get()->where('id', $id)->execute();
|
||||
|
||||
$modelR->string = 'Update';
|
||||
$modelR->int = '321';
|
||||
|
|
@ -454,8 +425,8 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$modelR->datetime = new \DateTime('now');
|
||||
$modelR->datetime_null = null;
|
||||
|
||||
$id2 = BaseModelMapper::update($modelR);
|
||||
$modelR2 = BaseModelMapper::get($id2);
|
||||
$id2 = BaseModelMapper::update()->execute($modelR);
|
||||
$modelR2 = BaseModelMapper::get()->where('id', $id2)->execute();
|
||||
|
||||
self::assertEquals($modelR->string, $modelR2->string);
|
||||
self::assertEquals($modelR->int, $modelR2->int);
|
||||
|
|
@ -471,52 +442,22 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully updates a database entry from an array
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @group framework
|
||||
*/
|
||||
public function testUpdateArray() : void
|
||||
{
|
||||
$id = BaseModelMapper::createArray($this->modelArray);
|
||||
$modelR = BaseModelMapper::getArray($id);
|
||||
|
||||
$modelR['string'] = 'Update';
|
||||
$modelR['int'] = '321';
|
||||
$modelR['bool'] = true;
|
||||
$modelR['float'] = 3.15;
|
||||
$modelR['null'] = null;
|
||||
$modelR['datetime'] = new \DateTime('now');
|
||||
$modelR['datetime_null'] = null;
|
||||
|
||||
$id2 = BaseModelMapper::updateArray($modelR);
|
||||
$modelR2 = BaseModelMapper::getArray($id2);
|
||||
|
||||
self::assertEquals($modelR['string'], $modelR2['string']);
|
||||
self::assertEquals($modelR['int'], $modelR2['int']);
|
||||
self::assertEquals($modelR['bool'], $modelR2['bool']);
|
||||
self::assertEquals($modelR['float'], $modelR2['float']);
|
||||
self::assertEquals($modelR['null'], $modelR2['null']);
|
||||
self::assertEquals($modelR['datetime']->format('Y-m-d'), $modelR2['datetime']->format('Y-m-d'));
|
||||
self::assertNull($modelR2['datetime_null']);
|
||||
|
||||
/**
|
||||
* @todo Orange-Management/phpOMS#226
|
||||
* Test the update of a model with relations (update relations).
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* @testdox The datamapper successfully deletes a database entry from a model
|
||||
* @covers phpOMS\DataStorage\Database\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperAbstract
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DataMapperFactory
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\ReadMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\WriteMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\UpdateMapper
|
||||
* @covers phpOMS\DataStorage\Database\Mapper\DeleteMapper
|
||||
* @group framework
|
||||
*/
|
||||
public function testDelete() : void
|
||||
{
|
||||
/*
|
||||
$id = BaseModelMapper::create($this->model);
|
||||
$id = BaseModelMapper::create()->execute($this->model);
|
||||
BaseModelMapper::delete($this->model);
|
||||
$modelR = BaseModelMapper::get($id);
|
||||
$modelR = BaseModelMapper::get()->where('id', $id)->execute();
|
||||
|
||||
self::assertInstanceOf('phpOMS\tests\DataStorage\Database\TestModel\NullBaseModel', $modelR);
|
||||
*/
|
||||
|
|
@ -528,14 +469,16 @@ final class DataMapperAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public function testDeleteHasManyRelation() : void
|
||||
{
|
||||
$id1 = BaseModelMapper::create($this->model);
|
||||
$id1 = BaseModelMapper::create()->execute($this->model);
|
||||
|
||||
$count1 = \count($this->model->hasManyRelations);
|
||||
|
||||
BaseModelMapper::deleteRelation('hasManyRelations', $id1, \reset($this->model->hasManyRelations)->id);
|
||||
/** @var ManyToManyRel $rel */
|
||||
$rel = \reset($this->model->hasManyRelations);
|
||||
|
||||
BaseModelMapper::clearCache();
|
||||
$base = BaseModelMapper::get($id1);
|
||||
BaseModelMapper::remover()->deleteRelationTable('hasManyRelations', [$rel->id], $id1);
|
||||
|
||||
$base = BaseModelMapper::get()->with('hasManyRelations')->where('id', $id1)->execute();
|
||||
|
||||
self::assertCount($count1 - 1, $base->hasManyRelations);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,12 @@
|
|||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\tests\DataStorage\Database\TestModel;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
class BaseModelMapper extends DataMapperAbstract
|
||||
class BaseModelMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -23,7 +24,7 @@ class BaseModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'test_base_id' => ['name' => 'test_base_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'test_base_string' => ['name' => 'test_base_string', 'type' => 'string', 'internal' => 'string', 'autocomplete' => true],
|
||||
'test_base_int' => ['name' => 'test_base_int', 'type' => 'int', 'internal' => 'int'],
|
||||
|
|
@ -44,14 +45,14 @@ class BaseModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{mapper:string, external:string}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $belongsTo = [
|
||||
public const BELONGS_TO = [
|
||||
'belongsToOne' => [
|
||||
'mapper' => BelongsToModelMapper::class,
|
||||
'external' => 'test_base_belongs_to_one',
|
||||
],
|
||||
];
|
||||
|
||||
protected static array $ownsOne = [
|
||||
public const OWNS_ONE = [
|
||||
'ownsOneSelf' => [
|
||||
'mapper' => OwnsOneModelMapper::class,
|
||||
'external' => 'test_base_owns_one_self',
|
||||
|
|
@ -64,7 +65,7 @@ class BaseModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{mapper:string, table:string, self?:?string, external?:?string, column?:string}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $hasMany = [
|
||||
public const HAS_MANY = [
|
||||
'hasManyDirect' => [
|
||||
'mapper' => ManyToManyDirectModelMapper::class,
|
||||
'table' => 'test_has_many_direct',
|
||||
|
|
@ -82,14 +83,13 @@ class BaseModelMapper extends DataMapperAbstract
|
|||
'table' => 'test_conditional',
|
||||
'self' => 'test_conditional_base',
|
||||
'column' => 'title',
|
||||
'conditional' => true,
|
||||
'external' => null,
|
||||
],
|
||||
];
|
||||
|
||||
protected static string $table = 'test_base';
|
||||
public const TABLE = 'test_base';
|
||||
|
||||
protected static string $createdAt = 'test_base_datetime';
|
||||
public const CREATED_AT = 'test_base_datetime';
|
||||
|
||||
protected static string $primaryField = 'test_base_id';
|
||||
public const PRIMARYFIELD ='test_base_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,12 @@
|
|||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\tests\DataStorage\Database\TestModel;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
class BelongsToModelMapper extends DataMapperAbstract
|
||||
class BelongsToModelMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -23,12 +24,12 @@ class BelongsToModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'test_belongs_to_one_id' => ['name' => 'test_belongs_to_one_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'test_belongs_to_one_string' => ['name' => 'test_belongs_to_one_string', 'type' => 'string', 'internal' => 'string'],
|
||||
];
|
||||
|
||||
protected static string $table = 'test_belongs_to_one';
|
||||
public const TABLE = 'test_belongs_to_one';
|
||||
|
||||
protected static string $primaryField = 'test_belongs_to_one_id';
|
||||
public const PRIMARYFIELD ='test_belongs_to_one_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace phpOMS\tests\DataStorage\Database\TestModel;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
/**
|
||||
* Tag mapper class.
|
||||
|
|
@ -24,7 +24,7 @@ use phpOMS\DataStorage\Database\DataMapperAbstract;
|
|||
* @link https://orange-management.org
|
||||
* @since 1.0.0
|
||||
*/
|
||||
final class ConditionalMapper extends DataMapperAbstract
|
||||
final class ConditionalMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -32,7 +32,7 @@ final class ConditionalMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'test_conditional_id' => ['name' => 'test_conditional_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'test_conditional_title' => ['name' => 'test_conditional_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true],
|
||||
'test_conditional_base' => ['name' => 'test_conditional_base', 'type' => 'int', 'internal' => 'base'],
|
||||
|
|
@ -45,7 +45,7 @@ final class ConditionalMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $table = 'test_conditional';
|
||||
public const TABLE = 'test_conditional';
|
||||
|
||||
/**
|
||||
* Primary field name.
|
||||
|
|
@ -53,5 +53,5 @@ final class ConditionalMapper extends DataMapperAbstract
|
|||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static string $primaryField = 'test_conditional_id';
|
||||
public const PRIMARYFIELD ='test_conditional_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@
|
|||
declare(strict_types=1);
|
||||
namespace phpOMS\tests\DataStorage\Database\TestModel;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
class ManyToManyDirectModelMapper extends DataMapperAbstract
|
||||
class ManyToManyDirectModelMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -23,13 +23,13 @@ class ManyToManyDirectModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'test_has_many_direct_id' => ['name' => 'test_has_many_direct_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'test_has_many_direct_string' => ['name' => 'test_has_many_direct_string', 'type' => 'string', 'internal' => 'string'],
|
||||
'test_has_many_direct_to' => ['name' => 'test_has_many_direct_to', 'type' => 'int', 'internal' => 'to'],
|
||||
];
|
||||
|
||||
protected static string $table = 'test_has_many_direct';
|
||||
public const TABLE = 'test_has_many_direct';
|
||||
|
||||
protected static string $primaryField = 'test_has_many_direct_id';
|
||||
public const PRIMARYFIELD ='test_has_many_direct_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,12 @@
|
|||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\tests\DataStorage\Database\TestModel;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
class ManyToManyRelModelMapper extends DataMapperAbstract
|
||||
class ManyToManyRelModelMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -23,12 +24,12 @@ class ManyToManyRelModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'test_has_many_rel_id' => ['name' => 'test_has_many_rel_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'test_has_many_rel_string' => ['name' => 'test_has_many_rel_string', 'type' => 'string', 'internal' => 'string'],
|
||||
];
|
||||
|
||||
protected static string $table = 'test_has_many_rel';
|
||||
public const TABLE = 'test_has_many_rel';
|
||||
|
||||
protected static string $primaryField = 'test_has_many_rel_id';
|
||||
public const PRIMARYFIELD ='test_has_many_rel_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,12 @@
|
|||
* @link https://orange-management.org
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace phpOMS\tests\DataStorage\Database\TestModel;
|
||||
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
|
||||
class OwnsOneModelMapper extends DataMapperAbstract
|
||||
class OwnsOneModelMapper extends DataMapperFactory
|
||||
{
|
||||
/**
|
||||
* Columns.
|
||||
|
|
@ -23,12 +24,12 @@ class OwnsOneModelMapper extends DataMapperAbstract
|
|||
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected static array $columns = [
|
||||
public const COLUMNS = [
|
||||
'test_owns_one_id' => ['name' => 'test_owns_one_id', 'type' => 'int', 'internal' => 'id'],
|
||||
'test_owns_one_string' => ['name' => 'test_owns_one_string', 'type' => 'string', 'internal' => 'string'],
|
||||
];
|
||||
|
||||
protected static string $table = 'test_owns_one';
|
||||
public const TABLE = 'test_owns_one';
|
||||
|
||||
protected static string $primaryField = 'test_owns_one_id';
|
||||
public const PRIMARYFIELD ='test_owns_one_id';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ namespace phpOMS\tests\Localization\Defaults;
|
|||
require_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
use phpOMS\DataStorage\Database\Connection\SQLiteConnection;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\Localization\Defaults\City;
|
||||
use phpOMS\Localization\Defaults\CityMapper;
|
||||
|
||||
/**
|
||||
|
|
@ -35,7 +36,7 @@ final class CityMapperTest extends \PHPUnit\Framework\TestCase
|
|||
'database' => \realpath(__DIR__ . '/../../../Localization/Defaults/localization.sqlite'),
|
||||
]);
|
||||
|
||||
DataMapperAbstract::setConnection($con);
|
||||
DataMapperFactory::db($con);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +46,8 @@ final class CityMapperTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
public function testR() : void
|
||||
{
|
||||
$obj = CityMapper::get(101079);
|
||||
/** @var City $obj */
|
||||
$obj = CityMapper::get()->where('id', 101079)->execute();
|
||||
self::assertEquals('DE', $obj->getCountryCode());
|
||||
self::assertEquals('Frankfurt', $obj->getName());
|
||||
self::assertEquals(60322, $obj->getPostal());
|
||||
|
|
@ -57,6 +59,6 @@ final class CityMapperTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public static function tearDownAfterClass() : void
|
||||
{
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ namespace phpOMS\tests\Localization\Defaults;
|
|||
require_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
use phpOMS\DataStorage\Database\Connection\SQLiteConnection;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\Localization\Defaults\Country;
|
||||
use phpOMS\Localization\Defaults\CountryMapper;
|
||||
|
||||
/**
|
||||
|
|
@ -35,7 +36,7 @@ final class CountryMapperTest extends \PHPUnit\Framework\TestCase
|
|||
'database' => \realpath(__DIR__ . '/../../../Localization/Defaults/localization.sqlite'),
|
||||
]);
|
||||
|
||||
DataMapperAbstract::setConnection($con);
|
||||
DataMapperFactory::db($con);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +46,8 @@ final class CountryMapperTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
public function testR() : void
|
||||
{
|
||||
$obj = CountryMapper::get(83);
|
||||
/** @var Country $obj */
|
||||
$obj = CountryMapper::get()->where('id', 83)->execute();
|
||||
self::assertEquals('Germany', $obj->getName());
|
||||
self::assertEquals('DE', $obj->getCode2());
|
||||
self::assertEquals('DEU', $obj->getCode3());
|
||||
|
|
@ -54,6 +56,6 @@ final class CountryMapperTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public static function tearDownAfterClass() : void
|
||||
{
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ namespace phpOMS\tests\Localization\Defaults;
|
|||
require_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
use phpOMS\DataStorage\Database\Connection\SQLiteConnection;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\Localization\Defaults\Currency;
|
||||
use phpOMS\Localization\Defaults\CurrencyMapper;
|
||||
|
||||
/**
|
||||
|
|
@ -35,7 +36,7 @@ final class CurrencyMapperTest extends \PHPUnit\Framework\TestCase
|
|||
'database' => \realpath(__DIR__ . '/../../../Localization/Defaults/localization.sqlite'),
|
||||
]);
|
||||
|
||||
DataMapperAbstract::setConnection($con);
|
||||
DataMapperFactory::db($con);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +46,8 @@ final class CurrencyMapperTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
public function testR() : void
|
||||
{
|
||||
$obj = CurrencyMapper::get(50);
|
||||
/** @var Currency $obj */
|
||||
$obj = CurrencyMapper::get()->where('id', 50)->execute();
|
||||
self::assertEquals('Euro', $obj->getName());
|
||||
self::assertEquals('EUR', $obj->getCode());
|
||||
self::assertEquals('978', $obj->getNumber());
|
||||
|
|
@ -57,6 +59,6 @@ final class CurrencyMapperTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public static function tearDownAfterClass() : void
|
||||
{
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ namespace phpOMS\tests\Localization\Defaults;
|
|||
require_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
use phpOMS\DataStorage\Database\Connection\SQLiteConnection;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\Localization\Defaults\Iban;
|
||||
use phpOMS\Localization\Defaults\IbanMapper;
|
||||
|
||||
/**
|
||||
|
|
@ -35,7 +36,7 @@ final class IbanMapperTest extends \PHPUnit\Framework\TestCase
|
|||
'database' => \realpath(__DIR__ . '/../../../Localization/Defaults/localization.sqlite'),
|
||||
]);
|
||||
|
||||
DataMapperAbstract::setConnection($con);
|
||||
DataMapperFactory::db($con);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +46,8 @@ final class IbanMapperTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
public function testR() : void
|
||||
{
|
||||
$obj = IbanMapper::get(22);
|
||||
/** @var Iban $obj */
|
||||
$obj = IbanMapper::get()->where('id', 22)->execute();
|
||||
self::assertEquals('DE', $obj->getCountry());
|
||||
self::assertEquals(22, $obj->getChars());
|
||||
self::assertEquals('18n', $obj->getBban());
|
||||
|
|
@ -54,6 +56,6 @@ final class IbanMapperTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public static function tearDownAfterClass() : void
|
||||
{
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ namespace phpOMS\tests\Localization\Defaults;
|
|||
require_once __DIR__ . '/../../Autoloader.php';
|
||||
|
||||
use phpOMS\DataStorage\Database\Connection\SQLiteConnection;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\Localization\Defaults\Language;
|
||||
use phpOMS\Localization\Defaults\LanguageMapper;
|
||||
|
||||
/**
|
||||
|
|
@ -35,7 +36,7 @@ final class LanguageMapperTest extends \PHPUnit\Framework\TestCase
|
|||
'database' => \realpath(__DIR__ . '/../../../Localization/Defaults/localization.sqlite'),
|
||||
]);
|
||||
|
||||
DataMapperAbstract::setConnection($con);
|
||||
DataMapperFactory::db($con);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -45,7 +46,8 @@ final class LanguageMapperTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
public function testR() : void
|
||||
{
|
||||
$obj = LanguageMapper::get(53);
|
||||
/** @var Language $obj */
|
||||
$obj = LanguageMapper::get()->where('id', 53)->execute();
|
||||
self::assertEquals('German', $obj->getName());
|
||||
self::assertEquals('Deutsch', $obj->getNative());
|
||||
self::assertEquals('de', $obj->getCode2());
|
||||
|
|
@ -55,6 +57,6 @@ final class LanguageMapperTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public static function tearDownAfterClass() : void
|
||||
{
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,21 +92,21 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
public function createRelationModel() : void
|
||||
{
|
||||
$model = new ManyToManyRelModel();
|
||||
ManyToManyRelModelMapper::create($model);
|
||||
ManyToManyRelModelMapper::create()->execute($model);
|
||||
}
|
||||
|
||||
public function createRelationDB() : void
|
||||
{
|
||||
$model1 = BaseModelMapper::get(1);
|
||||
$model2 = ManyToManyRelModelMapper::get(1);
|
||||
$model1 = BaseModelMapper::get()->where('id', 1)->execute();
|
||||
$model2 = ManyToManyRelModelMapper::get()->where('id', 1)->execute();
|
||||
|
||||
$this->createModelRelation(1, $model1->getId(), $model2->id, BaseModelMapper::class, 'hasManyRelations', '', '127.0.0.1');
|
||||
}
|
||||
|
||||
public function deleteRelationDB() : void
|
||||
{
|
||||
$model1 = BaseModelMapper::get(1);
|
||||
$model2 = ManyToManyRelModelMapper::get(1);
|
||||
$model1 = BaseModelMapper::get()->where('id', 1)->execute();
|
||||
$model2 = ManyToManyRelModelMapper::get()->where('id', 1)->execute();
|
||||
|
||||
$this->deleteModelRelation(1, $model1->getId(), $model2->id, BaseModelMapper::class, 'hasManyRelations', '', '127.0.0.1');
|
||||
}
|
||||
|
|
@ -121,7 +121,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
public function update() : void
|
||||
{
|
||||
$old = new BaseModel();
|
||||
BaseModelMapper::create($old);
|
||||
BaseModelMapper::create()->execute($old);
|
||||
|
||||
$new = clone $old;
|
||||
$new->string = 'Updated';
|
||||
|
|
@ -131,7 +131,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
public function delete() : void
|
||||
{
|
||||
$model = BaseModelMapper::get(1);
|
||||
$model = BaseModelMapper::get()->where('id', 1)->execute();
|
||||
$this->deleteModel(1, $model, BaseModelMapper::class, '', '127.0.0.1');
|
||||
}
|
||||
|
||||
|
|
@ -283,8 +283,6 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
*/
|
||||
private function dbSetup() : void
|
||||
{
|
||||
BaseModelMapper::clearCache();
|
||||
|
||||
$GLOBALS['dbpool']->get()->con->prepare(
|
||||
'CREATE TABLE `test_base` (
|
||||
`test_base_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
|
|
@ -368,7 +366,6 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_has_many_direct')->execute();
|
||||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_has_many_rel')->execute();
|
||||
$GLOBALS['dbpool']->get()->con->prepare('DROP TABLE test_has_many_rel_relations')->execute();
|
||||
BaseModelMapper::clearCache();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -381,7 +378,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$this->dbSetup();
|
||||
|
||||
$this->module->create();
|
||||
self::assertCount(1, BaseModelMapper::getAll());
|
||||
self::assertCount(1, BaseModelMapper::getAll()->execute());
|
||||
|
||||
$this->dbTeardown();
|
||||
}
|
||||
|
|
@ -396,7 +393,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$this->dbSetup();
|
||||
|
||||
$this->module->createMultiple();
|
||||
self::assertCount(2, BaseModelMapper::getAll());
|
||||
self::assertCount(2, BaseModelMapper::getAll()->execute());
|
||||
|
||||
$this->dbTeardown();
|
||||
}
|
||||
|
|
@ -411,7 +408,7 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$this->dbSetup();
|
||||
|
||||
$this->module->update();
|
||||
$updated = BaseModelMapper::get(1);
|
||||
$updated = BaseModelMapper::get()->where('id', 1)->execute();
|
||||
|
||||
self::assertEquals('Updated', $updated->string);
|
||||
|
||||
|
|
@ -428,9 +425,9 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$this->dbSetup();
|
||||
|
||||
$this->module->create();
|
||||
self::assertCount(1, BaseModelMapper::getAll());
|
||||
self::assertCount(1, BaseModelMapper::getAll()->execute());
|
||||
$this->module->delete();
|
||||
self::assertCount(0, BaseModelMapper::getAll());
|
||||
self::assertCount(0, BaseModelMapper::getAll()->execute());
|
||||
|
||||
$this->dbTeardown();
|
||||
}
|
||||
|
|
@ -448,14 +445,12 @@ final class ModuleAbstractTest extends \PHPUnit\Framework\TestCase
|
|||
$this->module->createRelationModel();
|
||||
$this->module->createRelationDB();
|
||||
|
||||
$model = BaseModelMapper::get(1);
|
||||
$model = BaseModelMapper::get()->with('hasManyRelations')->where('id', 1)->execute();
|
||||
self::assertCount(1, $model->hasManyRelations);
|
||||
|
||||
BaseModelMapper::clearCache();
|
||||
$this->module->deleteRelationDB();
|
||||
BaseModelMapper::clearCache();
|
||||
|
||||
$model = BaseModelMapper::get(1);
|
||||
$model = BaseModelMapper::get()->with('hasManyRelations')->where('id', 1)->execute();
|
||||
|
||||
// count = 2 because the moduel automatically initializes 2 hasMany relationships in the __construct()
|
||||
// This actually means that the delete was successful, otherwise the hasManyRelations would have been overwritten with 1 relation (see above before the delete)
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ final class ModuleManagerTest extends \PHPUnit\Framework\TestCase
|
|||
$module->id = 'TestModule';
|
||||
$module->name = 'TestModule';
|
||||
$module->path = 'TestModule';
|
||||
ModuleMapper::create($module);
|
||||
ModuleMapper::create()->execute($module);
|
||||
|
||||
self::assertTrue($this->moduleManager->deactivate('TestModule'));
|
||||
self::assertFalse($this->moduleManager->isActive('TestModule'));
|
||||
|
|
@ -181,7 +181,7 @@ final class ModuleManagerTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
// this is normally done in the ApiController
|
||||
$module->setStatus(ModuleStatus::ACTIVE);
|
||||
ModuleMapper::update($module);
|
||||
ModuleMapper::update()->execute($module);
|
||||
|
||||
$queryLoad = new Builder($this->app->dbPool->get('insert'));
|
||||
$queryLoad->insert('module_load_pid', 'module_load_type', 'module_load_from', 'module_load_for', 'module_load_file')
|
||||
|
|
@ -328,9 +328,8 @@ final class ModuleManagerTest extends \PHPUnit\Framework\TestCase
|
|||
|
||||
self::assertFalse($this->moduleManager->uninstall('TestModule'));
|
||||
|
||||
$module = ModuleMapper::get('TestModule');
|
||||
ModuleMapper::delete($module);
|
||||
ModuleMapper::clearCache();
|
||||
$module = ModuleMapper::get()->where('id', 'TestModule')->execute();
|
||||
ModuleMapper::delete()->execute($module);
|
||||
|
||||
self::assertFalse($this->moduleManager->isActive('TestModule'));
|
||||
self::assertFalse($this->moduleManager->isRunning('TestModule'));
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ use phpOMS\Account\AccountManager;
|
|||
use phpOMS\Application\ApplicationAbstract;
|
||||
use phpOMS\DataStorage\Cache\CachePool;
|
||||
use phpOMS\DataStorage\Database\DatabasePool;
|
||||
use phpOMS\DataStorage\Database\DataMapperAbstract;
|
||||
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
|
||||
use phpOMS\DataStorage\Session\HttpSession;
|
||||
use phpOMS\Dispatcher\Dispatcher;
|
||||
use phpOMS\Event\EventManager;
|
||||
|
|
@ -42,7 +42,7 @@ $GLOBALS['dbpool']->create('schema', $config['db']['core']['masters']['schema'])
|
|||
$httpSession = new HttpSession();
|
||||
$GLOBALS['session'] = $httpSession;
|
||||
|
||||
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
|
||||
DataMapperFactory::db($GLOBALS['dbpool']->get());
|
||||
|
||||
$app = new class() extends ApplicationAbstract
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user