change self/external behaviour / invert it!

This commit is contained in:
Dennis Eichhorn 2020-10-21 22:32:33 +02:00
parent 3df0d1463a
commit c2334057b3
2 changed files with 50 additions and 50 deletions

View File

@ -703,7 +703,7 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
public static function createRelation(string $member, $id1, $id2) : bool public static function createRelation(string $member, $id1, $id2) : bool
{ {
if (!isset(static::$hasMany[$member]) || !isset(static::$hasMany[$member]['self'])) { if (!isset(static::$hasMany[$member]) || !isset(static::$hasMany[$member]['external'])) {
return false; return false;
} }
@ -747,7 +747,7 @@ class DataMapperAbstract implements DataMapperInterface
if (\is_object($values)) { if (\is_object($values)) {
// conditionals // conditionals
$relReflectionClass = new \ReflectionClass($values); $relReflectionClass = new \ReflectionClass($values);
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['external']]['internal']); $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']);
if (!$isPublic) { if (!$isPublic) {
$relProperty->setAccessible(true); $relProperty->setAccessible(true);
@ -795,8 +795,8 @@ class DataMapperAbstract implements DataMapperInterface
// Setting relation value (id) for relation (since the relation is not stored in an extra relation table) // Setting relation value (id) for relation (since the relation is not stored in an extra relation table)
/** @var string $table */ /** @var string $table */
/** @var array $columns */ /** @var array $columns */
if (!isset(static::$hasMany[$propertyName]['self'])) { if (!isset(static::$hasMany[$propertyName]['external'])) {
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['external']]['internal']); $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']);
if (!$isPublic) { if (!$isPublic) {
$relProperty->setAccessible(true); $relProperty->setAccessible(true);
@ -842,7 +842,7 @@ class DataMapperAbstract implements DataMapperInterface
if (\is_object($values)) { if (\is_object($values)) {
// conditionals // conditionals
TestUtils::setMember($values, $mapper::$columns[static::$hasMany[$propertyName]['external']]['internal'], $objId); TestUtils::setMember($values, $mapper::$columns[static::$hasMany[$propertyName]['self']]['internal'], $objId);
$mapper::createArray($values); $mapper::createArray($values);
continue; continue;
@ -876,9 +876,9 @@ class DataMapperAbstract implements DataMapperInterface
/** @var string $table */ /** @var string $table */
/** @var array $columns */ /** @var array $columns */
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table
&& isset($mapper::$columns[static::$hasMany[$propertyName]['external']]) && isset($mapper::$columns[static::$hasMany[$propertyName]['self']])
) { ) {
$value[$mapper::$columns[static::$hasMany[$propertyName]['external']]['internal']] = $objId; $value[$mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']] = $objId;
} }
$objsIds[$key] = $mapper::createArray($value); $objsIds[$key] = $mapper::createArray($value);
@ -1012,13 +1012,13 @@ class DataMapperAbstract implements DataMapperInterface
private static function createRelationTable(string $propertyName, array $objsIds, $objId) : void private static function createRelationTable(string $propertyName, array $objsIds, $objId) : void
{ {
/** @var string $table */ /** @var string $table */
if (empty($objsIds) || !isset(static::$hasMany[$propertyName]['self'])) { if (empty($objsIds) || !isset(static::$hasMany[$propertyName]['external'])) {
return; return;
} }
$relQuery = new Builder(self::$db); $relQuery = new Builder(self::$db);
$relQuery->into(static::$hasMany[$propertyName]['table']) $relQuery->into(static::$hasMany[$propertyName]['table'])
->insert(static::$hasMany[$propertyName]['self'], static::$hasMany[$propertyName]['external']); ->insert(static::$hasMany[$propertyName]['external'], static::$hasMany[$propertyName]['self']);
foreach ($objsIds as $key => $src) { foreach ($objsIds as $key => $src) {
if (\is_object($src)) { if (\is_object($src)) {
@ -1148,9 +1148,9 @@ class DataMapperAbstract implements DataMapperInterface
/** @var string $table */ /** @var string $table */
/** @var array $columns */ /** @var array $columns */
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table
&& isset($mapper::$columns[static::$hasMany[$propertyName]['external']]) && isset($mapper::$columns[static::$hasMany[$propertyName]['self']])
) { ) {
$relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['external']]['internal']); $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']);
if (!$isPublic) { if (!$isPublic) {
$relProperty->setAccessible(true); $relProperty->setAccessible(true);
@ -1230,9 +1230,9 @@ class DataMapperAbstract implements DataMapperInterface
/** @var string $table */ /** @var string $table */
/** @var array $columns */ /** @var array $columns */
if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table if (static::$hasMany[$propertyName]['table'] === static::$hasMany[$propertyName]['mapper']::$table
&& isset($mapper::$columns[static::$hasMany[$propertyName]['external']]) && isset($mapper::$columns[static::$hasMany[$propertyName]['self']])
) { ) {
$value[$mapper::$columns[static::$hasMany[$propertyName]['external']]['internal']] = $objId; $value[$mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']] = $objId;
} }
$objsIds[$propertyName][$key] = $mapper::createArray($value); $objsIds[$propertyName][$key] = $mapper::createArray($value);
@ -1297,8 +1297,8 @@ class DataMapperAbstract implements DataMapperInterface
$relQuery = new Builder(self::$db); $relQuery = new Builder(self::$db);
$relQuery->delete() $relQuery->delete()
->from(static::$hasMany[$propertyName]['table']) ->from(static::$hasMany[$propertyName]['table'])
->where(static::$hasMany[$propertyName]['table'] . '.' . static::$hasMany[$propertyName]['self'], '=', $src) ->where(static::$hasMany[$propertyName]['table'] . '.' . static::$hasMany[$propertyName]['external'], '=', $src)
->where(static::$hasMany[$propertyName]['table'] . '.' . static::$hasMany[$propertyName]['external'], '=', $objId, 'and'); ->where(static::$hasMany[$propertyName]['table'] . '.' . static::$hasMany[$propertyName]['self'], '=', $objId, 'and');
self::$db->con->prepare($relQuery->toSql())->execute(); self::$db->con->prepare($relQuery->toSql())->execute();
} }
@ -2017,10 +2017,10 @@ class DataMapperAbstract implements DataMapperInterface
$mapper = static::$ownsOne[$member]['mapper']; $mapper = static::$ownsOne[$member]['mapper'];
if ($depth < 1) { if ($depth < 1) {
if (\array_key_exists(static::$ownsOne[$member]['self'] . '_' . ($depth + 1), $result)) { if (\array_key_exists(static::$ownsOne[$member]['external'] . '_' . ($depth + 1), $result)) {
return isset(static::$ownsOne[$member]['column']) return isset(static::$ownsOne[$member]['column'])
? $result[static::$ownsOne[$member]['self'] . '_' . ($depth + 1)] ? $result[static::$ownsOne[$member]['external'] . '_' . ($depth + 1)]
: $mapper::createNullModel($result[static::$ownsOne[$member]['self'] . '_' . ($depth + 1)]); : $mapper::createNullModel($result[static::$ownsOne[$member]['external'] . '_' . ($depth + 1)]);
} else { } else {
return $default; return $default;
} }
@ -2057,8 +2057,8 @@ class DataMapperAbstract implements DataMapperInterface
$mapper = static::$ownsOne[$member]['mapper']; $mapper = static::$ownsOne[$member]['mapper'];
if ($depth < 1) { if ($depth < 1) {
if (\array_key_exists(static::$ownsOne[$member]['self'] . '_' . ($depth + 1), $result)) { if (\array_key_exists(static::$ownsOne[$member]['external'] . '_' . ($depth + 1), $result)) {
return $result[static::$ownsOne[$member]['self'] . '_' . ($depth + 1)]; return $result[static::$ownsOne[$member]['external'] . '_' . ($depth + 1)];
} else { } else {
return $default; return $default;
} }
@ -2097,10 +2097,10 @@ class DataMapperAbstract implements DataMapperInterface
$mapper = static::$belongsTo[$member]['mapper']; $mapper = static::$belongsTo[$member]['mapper'];
if ($depth < 1) { if ($depth < 1) {
if (\array_key_exists(static::$belongsTo[$member]['self'] . '_' . ($depth + 1), $result)) { if (\array_key_exists(static::$belongsTo[$member]['external'] . '_' . ($depth + 1), $result)) {
return isset(static::$belongsTo[$member]['column']) return isset(static::$belongsTo[$member]['column'])
? $result[static::$belongsTo[$member]['self'] . '_' . ($depth + 1)] ? $result[static::$belongsTo[$member]['external'] . '_' . ($depth + 1)]
: $mapper::createNullModel($result[static::$belongsTo[$member]['self'] . '_' . ($depth + 1)]); : $mapper::createNullModel($result[static::$belongsTo[$member]['external'] . '_' . ($depth + 1)]);
} else { } else {
return $default; return $default;
} }
@ -2137,8 +2137,8 @@ class DataMapperAbstract implements DataMapperInterface
$mapper = static::$belongsTo[$member]['mapper']; $mapper = static::$belongsTo[$member]['mapper'];
if ($depth < 1) { if ($depth < 1) {
if (\array_key_exists(static::$belongsTo[$member]['self'] . '_' . ($depth + 1), $result)) { if (\array_key_exists(static::$belongsTo[$member]['external'] . '_' . ($depth + 1), $result)) {
return $result[static::$belongsTo[$member]['self'] . '_' . ($depth + 1)]; return $result[static::$belongsTo[$member]['external'] . '_' . ($depth + 1)];
} else { } else {
return $default; return $default;
} }
@ -2973,42 +2973,42 @@ class DataMapperAbstract implements DataMapperInterface
} else { } else {
if (isset(static::$hasMany[$ref])) { if (isset(static::$hasMany[$ref])) {
if ($keys !== null && $keys !== false) { if ($keys !== null && $keys !== false) {
$query->where(static::$hasMany[$ref]['table'] . '_' . $depth . '.' . static::$hasMany[$ref]['self'], $comparison, $keys); $query->where(static::$hasMany[$ref]['table'] . '_' . $depth . '.' . static::$hasMany[$ref]['external'], $comparison, $keys);
} }
$query->leftJoin(static::$hasMany[$ref]['table'], static::$hasMany[$ref]['table'] . '_' . $depth); $query->leftJoin(static::$hasMany[$ref]['table'], static::$hasMany[$ref]['table'] . '_' . $depth);
if (static::$hasMany[$ref]['self'] !== null) { if (static::$hasMany[$ref]['external'] !== null) {
$query->on( $query->on(
static::$table . '_' . $depth . '.' . static::$hasMany[$ref][static::$primaryField], '=', static::$table . '_' . $depth . '.' . static::$hasMany[$ref][static::$primaryField], '=',
static::$hasMany[$ref]['table'] . '_' . $depth . '.' . static::$hasMany[$ref]['external'], 'and', static::$hasMany[$ref]['table'] . '_' . $depth . '.' . static::$hasMany[$ref]['self'], 'and',
static::$hasMany[$ref]['table'] . '_' . $depth static::$hasMany[$ref]['table'] . '_' . $depth
); );
} else { } else {
$query->on( $query->on(
static::$table . '_' . $depth . '.' . static::$primaryField, '=', static::$table . '_' . $depth . '.' . static::$primaryField, '=',
static::$hasMany[$ref]['table'] . '_' . $depth . '.' . static::$hasMany[$ref]['external'], 'and', static::$hasMany[$ref]['table'] . '_' . $depth . '.' . static::$hasMany[$ref]['self'], 'and',
static::$hasMany[$ref]['table'] . '_' . $depth static::$hasMany[$ref]['table'] . '_' . $depth
); );
} }
} elseif (isset(static::$belongsTo[$ref]) && static::$belongsTo[$ref]['self'] !== null) { } elseif (isset(static::$belongsTo[$ref]) && static::$belongsTo[$ref]['external'] !== null) {
if ($keys !== null && $keys !== false) { if ($keys !== null && $keys !== false) {
$query->where(static::$table . '_' . $depth . '.' . $ref, $comparison, $keys); $query->where(static::$table . '_' . $depth . '.' . $ref, $comparison, $keys);
} }
$query->leftJoin(static::$belongsTo[$ref]['mapper']::getTable(), static::$belongsTo[$ref]['mapper']::getTable() . '_' . $depth) $query->leftJoin(static::$belongsTo[$ref]['mapper']::getTable(), static::$belongsTo[$ref]['mapper']::getTable() . '_' . $depth)
->on( ->on(
static::$table . '_' . $depth . '.' . static::$belongsTo[$ref]['self'], '=', static::$table . '_' . $depth . '.' . static::$belongsTo[$ref]['external'], '=',
static::$belongsTo[$ref]['mapper']::getTable() . '_' . $depth . '.' . static::$belongsTo[$ref]['mapper']::getPrimaryField() , 'and', static::$belongsTo[$ref]['mapper']::getTable() . '_' . $depth . '.' . static::$belongsTo[$ref]['mapper']::getPrimaryField() , 'and',
static::$belongsTo[$ref]['mapper']::getTable() . '_' . $depth static::$belongsTo[$ref]['mapper']::getTable() . '_' . $depth
); );
} elseif (isset(static::$ownsOne[$ref]) && static::$ownsOne[$ref]['self'] !== null) { } elseif (isset(static::$ownsOne[$ref]) && static::$ownsOne[$ref]['external'] !== null) {
if ($keys !== null && $keys !== false) { if ($keys !== null && $keys !== false) {
$query->where(static::$table . '_' . $depth . '.' . $ref, $comparison, $keys); $query->where(static::$table . '_' . $depth . '.' . $ref, $comparison, $keys);
} }
$query->leftJoin(static::$ownsOne[$ref]['mapper']::getTable(), static::$ownsOne[$ref]['mapper']::getTable() . '_' . $depth) $query->leftJoin(static::$ownsOne[$ref]['mapper']::getTable(), static::$ownsOne[$ref]['mapper']::getTable() . '_' . $depth)
->on( ->on(
static::$table . '_' . $depth . '.' . static::$ownsOne[$ref]['self'], '=', static::$table . '_' . $depth . '.' . static::$ownsOne[$ref]['external'], '=',
static::$ownsOne[$ref]['mapper']::getTable() . '_' . $depth . '.' . static::$ownsOne[$ref]['mapper']::getPrimaryField() , 'and', static::$ownsOne[$ref]['mapper']::getTable() . '_' . $depth . '.' . static::$ownsOne[$ref]['mapper']::getPrimaryField() , 'and',
static::$ownsOne[$ref]['mapper']::getTable() . '_' . $depth static::$ownsOne[$ref]['mapper']::getTable() . '_' . $depth
); );
@ -3041,9 +3041,9 @@ class DataMapperAbstract implements DataMapperInterface
public static function getHasManyPrimaryKeys($refKey, string $ref) : array public static function getHasManyPrimaryKeys($refKey, string $ref) : array
{ {
$query = new Builder(self::$db); $query = new Builder(self::$db);
$query->select(static::$hasMany[$ref]['table'] . '.' . static::$hasMany[$ref]['external']) $query->select(static::$hasMany[$ref]['table'] . '.' . static::$hasMany[$ref]['self'])
->from(static::$hasMany[$ref]['table']) ->from(static::$hasMany[$ref]['table'])
->where(static::$hasMany[$ref]['table'] . '.' . static::$hasMany[$ref]['self'], '=', $refKey); ->where(static::$hasMany[$ref]['table'] . '.' . static::$hasMany[$ref]['external'], '=', $refKey);
$sth = self::$db->con->prepare($query->toSql()); $sth = self::$db->con->prepare($query->toSql());
$sth->execute(); $sth->execute();
@ -3080,11 +3080,11 @@ class DataMapperAbstract implements DataMapperInterface
if (self::$relations === RelationType::ALL) { if (self::$relations === RelationType::ALL) {
/** @var string $primaryField */ /** @var string $primaryField */
$src = $value['self'] ?? $value['mapper']::$primaryField; $src = $value['external'] ?? $value['mapper']::$primaryField;
$query->select($value['table'] . '.' . $src) $query->select($value['table'] . '.' . $src)
->from($value['table']) ->from($value['table'])
->where($value['table'] . '.' . $value['external'], '=', $primaryKey); ->where($value['table'] . '.' . $value['self'], '=', $primaryKey);
$modelName = $value['mapper']::getModelName(); $modelName = $value['mapper']::getModelName();
foreach (self::$conditionals as $condKey => $condValue) { foreach (self::$conditionals as $condKey => $condValue) {
@ -3159,7 +3159,7 @@ class DataMapperAbstract implements DataMapperInterface
foreach (static::$ownsOne as $rel) { foreach (static::$ownsOne as $rel) {
$query->leftJoin($rel['mapper']::getTable(), $rel['mapper']::getTable() . '_' . ($depth - 1)) $query->leftJoin($rel['mapper']::getTable(), $rel['mapper']::getTable() . '_' . ($depth - 1))
->on( ->on(
static::$table . '_' . $depth . '.' . $rel['self'], '=', static::$table . '_' . $depth . '.' . $rel['external'], '=',
$rel['mapper']::getTable() . '_' . ($depth - 1) . '.' . ( $rel['mapper']::getTable() . '_' . ($depth - 1) . '.' . (
isset($rel['by']) ? $rel['mapper']::getColumnByMember($rel['by']) : $rel['mapper']::getPrimaryField() isset($rel['by']) ? $rel['mapper']::getColumnByMember($rel['by']) : $rel['mapper']::getPrimaryField()
), 'and', ), 'and',
@ -3180,7 +3180,7 @@ class DataMapperAbstract implements DataMapperInterface
foreach (static::$belongsTo as $rel) { foreach (static::$belongsTo as $rel) {
$query->leftJoin($rel['mapper']::getTable(), $rel['mapper']::getTable() . '_' . ($depth - 1)) $query->leftJoin($rel['mapper']::getTable(), $rel['mapper']::getTable() . '_' . ($depth - 1))
->on( ->on(
static::$table . '_' . $depth . '.' . $rel['self'], '=', static::$table . '_' . $depth . '.' . $rel['external'], '=',
$rel['mapper']::getTable() . '_' . ($depth - 1) . '.' . ( $rel['mapper']::getTable() . '_' . ($depth - 1) . '.' . (
isset($rel['by']) ? $rel['mapper']::getColumnByMember($rel['by']) : $rel['mapper']::getPrimaryField() isset($rel['by']) ? $rel['mapper']::getColumnByMember($rel['by']) : $rel['mapper']::getPrimaryField()
), 'and', ), 'and',
@ -3199,16 +3199,16 @@ class DataMapperAbstract implements DataMapperInterface
// get HasManyQuery (but only for elements which have a 'column' defined) // get HasManyQuery (but only for elements which have a 'column' defined)
if ($depth > 1 && self::$relations === RelationType::ALL) { if ($depth > 1 && self::$relations === RelationType::ALL) {
foreach (static::$hasMany as $rel) { foreach (static::$hasMany as $rel) {
if (isset($rel['self']) || !isset($rel['column'])) { // @todo: conflict with getHasMany()???!?!?!?! if (isset($rel['external']) || !isset($rel['column'])) { // @todo: conflict with getHasMany()???!?!?!?!
continue; continue;
} }
// todo: handle self and self === null // todo: handle self and self === null
$query->leftJoin($rel['mapper']::getTable(), $rel['mapper']::getTable() . '_' . ($depth - 1)) $query->leftJoin($rel['mapper']::getTable(), $rel['mapper']::getTable() . '_' . ($depth - 1))
->on( ->on(
static::$table . '_' . $depth . '.' . ($rel['self'] ?? static::$primaryField), '=', static::$table . '_' . $depth . '.' . ($rel['external'] ?? static::$primaryField), '=',
$rel['mapper']::getTable() . '_' . ($depth - 1) . '.' . ( $rel['mapper']::getTable() . '_' . ($depth - 1) . '.' . (
isset($rel['by']) ? $rel['mapper']::getColumnByMember($rel['by']) : $rel['external'] isset($rel['by']) ? $rel['mapper']::getColumnByMember($rel['by']) : $rel['self']
), 'and', ), 'and',
$rel['mapper']::getTable() . '_' . ($depth - 1) $rel['mapper']::getTable() . '_' . ($depth - 1)
); );

View File

@ -47,14 +47,14 @@ class BaseModelMapper extends DataMapperAbstract
protected static array $belongsTo = [ protected static array $belongsTo = [
'belongsToOne' => [ 'belongsToOne' => [
'mapper' => BelongsToModelMapper::class, 'mapper' => BelongsToModelMapper::class,
'self' => 'test_base_belongs_to_one', 'external' => 'test_base_belongs_to_one',
], ],
]; ];
protected static array $ownsOne = [ protected static array $ownsOne = [
'ownsOneSelf' => [ 'ownsOneSelf' => [
'mapper' => OwnsOneModelMapper::class, 'mapper' => OwnsOneModelMapper::class,
'self' => 'test_base_owns_one_self', 'external' => 'test_base_owns_one_self',
], ],
]; ];
@ -68,22 +68,22 @@ class BaseModelMapper extends DataMapperAbstract
'hasManyDirect' => [ 'hasManyDirect' => [
'mapper' => ManyToManyDirectModelMapper::class, 'mapper' => ManyToManyDirectModelMapper::class,
'table' => 'test_has_many_direct', 'table' => 'test_has_many_direct',
'external' => 'test_has_many_direct_to', 'self' => 'test_has_many_direct_to',
'self' => null, 'external' => null,
], ],
'hasManyRelations' => [ 'hasManyRelations' => [
'mapper' => ManyToManyRelModelMapper::class, 'mapper' => ManyToManyRelModelMapper::class,
'table' => 'test_has_many_rel_relations', 'table' => 'test_has_many_rel_relations',
'external' => 'test_has_many_rel_relations_dest', 'external' => 'test_has_many_rel_relations_src',
'self' => 'test_has_many_rel_relations_src', 'self' => 'test_has_many_rel_relations_dest',
], ],
'conditional' => [ 'conditional' => [
'mapper' => ConditionalMapper::class, 'mapper' => ConditionalMapper::class,
'table' => 'test_conditional', 'table' => 'test_conditional',
'external' => 'test_conditional_base', 'self' => 'test_conditional_base',
'column' => 'title', 'column' => 'title',
'conditional' => true, 'conditional' => true,
'self' => null, 'external' => null,
], ],
]; ];