fix ownsOne and belongsTo hasMany relationships

This commit is contained in:
Dennis Eichhorn 2020-03-30 22:12:49 +02:00
parent ee06a96792
commit 3997d9563e

View File

@ -915,8 +915,12 @@ class DataMapperAbstract implements DataMapperInterface
foreach ($objsIds as $key => $src) { foreach ($objsIds as $key => $src) {
if (\is_object($src)) { if (\is_object($src)) {
$mapper = \get_class($src) . 'Mapper'; $mapper = (\stripos($mapper = \get_class($src), '\Null') !== false
$src = $mapper::getObjectId($src); ? \str_replace('\Null', '\\', $mapper)
: $mapper)
. 'Mapper';
$src = $mapper::getObjectId($src);
} }
$relQuery->values($src, $objId); $relQuery->values($src, $objId);
@ -2082,6 +2086,10 @@ class DataMapperAbstract implements DataMapperInterface
$value = self::populateOwnsOne($def['internal'], $result, $depth - 1, $default); $value = self::populateOwnsOne($def['internal'], $result, $depth - 1, $default);
if (\is_object($value) && isset(static::$ownsOne[$def['internal']]['mapper'])) {
static::$ownsOne[$def['internal']]['mapper']::fillRelations($value, RelationType::ALL, $depth - 1);
}
$refProp->setValue($obj, $value); $refProp->setValue($obj, $value);
} elseif (isset(static::$belongsTo[$def['internal']])) { } elseif (isset(static::$belongsTo[$def['internal']])) {
$default = null; $default = null;
@ -2091,6 +2099,10 @@ class DataMapperAbstract implements DataMapperInterface
$value = self::populateBelongsTo($def['internal'], $result, $depth - 1, $default); $value = self::populateBelongsTo($def['internal'], $result, $depth - 1, $default);
if (\is_object($value) && isset(static::$belongsTo[$def['internal']]['mapper'])) {
static::$belongsTo[$def['internal']]['mapper']::fillRelations($value, RelationType::ALL, $depth - 1);
}
$refProp->setValue($obj, $value); $refProp->setValue($obj, $value);
} elseif (\in_array($def['type'], ['string', 'int', 'float', 'bool'])) { } elseif (\in_array($def['type'], ['string', 'int', 'float', 'bool'])) {
if ($value !== null || $refProp->getValue($obj) !== null) { if ($value !== null || $refProp->getValue($obj) !== null) {
@ -2223,8 +2235,12 @@ class DataMapperAbstract implements DataMapperInterface
if (isset(static::$ownsOne[$def['internal']])) { if (isset(static::$ownsOne[$def['internal']])) {
$value = self::populateOwnsOneArray(static::$columns[$column]['internal'], $result, $depth - 1); $value = self::populateOwnsOneArray(static::$columns[$column]['internal'], $result, $depth - 1);
static::$ownsOne[$def['internal']]['mapper']::fillRelationsArray($value, RelationType::ALL, $depth - 1);
} elseif (isset(static::$belongsTo[$def['internal']])) { } elseif (isset(static::$belongsTo[$def['internal']])) {
$value = self::populateBelongsToArray(static::$columns[$column]['internal'], $result, $depth - 1); $value = self::populateBelongsToArray(static::$columns[$column]['internal'], $result, $depth - 1);
static::$belongsTo[$def['internal']]['mapper']::fillRelationsArray($value, RelationType::ALL, $depth - 1);
} elseif (\in_array(static::$columns[$column]['type'], ['string', 'int', 'float', 'bool'])) { } elseif (\in_array(static::$columns[$column]['type'], ['string', 'int', 'float', 'bool'])) {
\settype($value, static::$columns[$column]['type']); \settype($value, static::$columns[$column]['type']);
} elseif (static::$columns[$column]['type'] === 'DateTime') { } elseif (static::$columns[$column]['type'] === 'DateTime') {
@ -2423,10 +2439,13 @@ class DataMapperAbstract implements DataMapperInterface
self::addInitialized(static::class, $value, $obj[$value]); self::addInitialized(static::class, $value, $obj[$value]);
$obj[$value] = self::populateAbstract($row, $obj[$value], $depth); $obj[$value] = self::populateAbstract($row, $obj[$value], $depth);
foreach (static::$hasMany as $many) {
self::fillRelations($obj[$value], $relations, $depth - 1);
}
} }
} }
self::fillRelations($obj, $relations, $depth - 1);
self::clear(); self::clear();
$countResulsts = \count($obj); $countResulsts = \count($obj);
@ -2491,9 +2510,12 @@ class DataMapperAbstract implements DataMapperInterface
$obj[$value] = self::populateAbstractArray($row, [], $depth); $obj[$value] = self::populateAbstractArray($row, [], $depth);
self::addInitializedArray(static::class, $value, $obj[$value]); self::addInitializedArray(static::class, $value, $obj[$value]);
foreach (static::$hasMany as $many) {
self::fillRelationsArray(${$obj}[$value], $relations, $depth - 1);
}
} }
self::fillRelationsArray($obj, $relations, $depth - 1);
self::clear(); self::clear();
return \count($obj) === 1 ? \reset($obj) : $obj; return \count($obj) === 1 ? \reset($obj) : $obj;
@ -2686,7 +2708,7 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Fill object with relations * Fill object with relations
* *
* @param array $obj Objects to fill * @param mixed $obj Object to fill
* @param int $relations Relations type * @param int $relations Relations type
* @param int $depth Relation depth * @param int $depth Relation depth
* *
@ -2694,7 +2716,7 @@ class DataMapperAbstract implements DataMapperInterface
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function fillRelations(array &$obj, int $relations = RelationType::ALL, int $depth = 3) : void public static function fillRelations($obj, int $relations = RelationType::ALL, int $depth = 3) : void
{ {
if ($depth < 1) { if ($depth < 1) {
return; return;
@ -2704,20 +2726,19 @@ class DataMapperAbstract implements DataMapperInterface
return; return;
} }
$hasMany = !empty(static::$hasMany); if (empty(static::$hasMany)) {
if (!$hasMany) {
return; return;
} }
foreach ($obj as $key => $value) { $key = self::getObjectId($obj);
/* loading relations from relations table and populating them and then adding them to the object */
if ($hasMany) { if (empty($key)) {
// todo: let hasmanyraw return the full data already and let populatemanytomany just fill it! return;
// todo: create a get has many raw id function because sometimes we need this (see update function)
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key], $depth);
}
} }
// todo: let hasmanyraw return the full data already and let populatemanytomany just fill it!
// todo: create a get has many raw id function because sometimes we need this (see update function)
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj, $depth);
} }
/** /**
@ -2741,18 +2762,18 @@ class DataMapperAbstract implements DataMapperInterface
return; return;
} }
$hasMany = !empty(static::$hasMany); if (empty(static::$hasMany)) {
if (!$hasMany) {
return; return;
} }
foreach ($obj as $key => $value) { $key = $obj[$mapper::$columns[$mapper::$primaryField]['internal']];
/* loading relations from relations table and populating them and then adding them to the object */
if ($hasMany) { if (empty($key)) {
self::populateManyToManyArray(self::getHasManyRaw($key, $relations), $obj[$key], $depth); return;
}
} }
/* loading relations from relations table and populating them and then adding them to the object */
self::populateManyToManyArray(self::getHasManyRaw($key, $mapper, $relations), $obj[$key], $depth);
} }
/** /**