diff --git a/Account/Account.php b/Account/Account.php index 0c889a5dc..ebaf8f959 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -144,7 +144,7 @@ class Account implements ArrayableInterface, \JsonSerializable * @var Localization * @since 1.0.0 */ - protected Localization $l11n; + protected Localization $localization; use PermissionHandlingTrait; @@ -159,10 +159,10 @@ class Account implements ArrayableInterface, \JsonSerializable */ public function __construct(int $id = 0) { - $this->createdAt = new \DateTime('now'); - $this->lastActive = new \DateTime('now'); - $this->id = $id; - $this->l11n = new Localization(); + $this->createdAt = new \DateTime('now'); + $this->lastActive = new \DateTime('now'); + $this->id = $id; + $this->localization = new Localization(); } /** @@ -188,7 +188,7 @@ class Account implements ArrayableInterface, \JsonSerializable */ public function getL11n() : Localization { - return $this->l11n; + return $this->localization; } /** @@ -231,7 +231,7 @@ class Account implements ArrayableInterface, \JsonSerializable */ public function setL11n(Localization $l11n) : void { - $this->l11n = $l11n; + $this->localization = $l11n; } /** diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index a80afe22c..d3d1203e2 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -2066,7 +2066,12 @@ class DataMapperAbstract implements DataMapperInterface $refProp->setAccessible(true); } - $objects = $mapper::get($values, RelationType::ALL, null, $depth); + if (!isset(static::$hasMany[$member]['by'])) { + $objects = $mapper::get($values, RelationType::ALL, null, $depth); + } else { + $objects = $mapper::getBy($values, static::$hasMany[$member]['by'], RelationType::ALL, null, $depth); + } + $refProp->setValue($obj, !\is_array($objects) ? [$objects->getId() => $objects] : $objects); if (!$accessible) { @@ -2129,8 +2134,13 @@ class DataMapperAbstract implements DataMapperInterface continue; } - $id = \is_object($id) ? self::getObjectId($id) : $id; - $value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth); + $id = \is_object($id) ? self::getObjectId($id) : $id; + + if (!isset(static::$ownsOne[$member]['by'])) { + $value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth); + } else { + $value = $mapper::getBy($id, static::$ownsOne[$member]['by'], RelationType::ALL, null, $depth); + } $refProp->setValue($obj, $value); @@ -2507,8 +2517,8 @@ class DataMapperAbstract implements DataMapperInterface /** * Get object. * - * @param mixed $refKey Key - * @param string $ref The field that defines the for + * @param mixed $forKey Key + * @param string $for The field that defines the for * @param int $relations Load relations * @param mixed $fill Object to fill * @param int $depth Relation depth @@ -2517,10 +2527,10 @@ class DataMapperAbstract implements DataMapperInterface * * @since 1.0.0 */ - public static function getFor($refKey, string $ref, int $relations = RelationType::ALL, $fill = null, int $depth = 3) + public static function getFor($forKey, string $for, int $relations = RelationType::ALL, $fill = null, int $depth = 3) { if ($depth < 1) { - return $refKey; + return $forKey; } if (!isset(self::$parentMapper)) { @@ -2529,16 +2539,68 @@ class DataMapperAbstract implements DataMapperInterface self::extend(__CLASS__); - $refKey = (array) $refKey; + $forKey = (array) $forKey; $obj = []; - foreach ($refKey as $key => $value) { + foreach ($forKey as $key => $value) { $toLoad = []; - if (isset(static::$hasMany[$ref]) && static::$hasMany[$ref]['src'] !== null) { - $toLoad = self::getHasManyPrimaryKeys($value, $ref); + if (isset(static::$hasMany[$for]) && static::$hasMany[$for]['src'] !== null) { + $toLoad = self::getHasManyPrimaryKeys($value, $for); } else { - $toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($ref)); + $toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($for)); + } + + $obj[$value] = self::get($toLoad, $relations, $fill, $depth); + } + + $countResulsts = \count($obj); + + if ($countResulsts === 0) { + return self::getNullModelObj(); + } elseif ($countResulsts === 1) { + return \reset($obj); + } + + return $obj; + } + + /** + * Get object. + * + * @param mixed $byKey Key + * @param string $by The field that defines the for + * @param int $relations Load relations + * @param mixed $fill Object to fill + * @param int $depth Relation depth + * + * @return mixed + * + * @since 1.0.0 + */ + public static function getBy($byKey, string $by, int $relations = RelationType::ALL, $fill = null, int $depth = 3) + { + if ($depth < 1) { + return $byKey; + } + + if (!isset(self::$parentMapper)) { + self::$parentMapper = static::class; + } + + self::extend(__CLASS__); + + $byKey = (array) $byKey; + $obj = []; + + foreach ($byKey as $key => $value) { + $toLoad = []; + + if (isset(static::$hasMany[$by]) && static::$hasMany[$by]['src'] !== null) { + // todo: maybe wrong?! + $toLoad = self::getHasManyPrimaryKeys($value, $by); + } elseif (isset(static::$ownsOne[$by])) { + $toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($by)); } $obj[$value] = self::get($toLoad, $relations, $fill, $depth); diff --git a/Localization/Defaults/City.php b/Localization/Defaults/City.php index f051a0a12..921b2c4cd 100644 --- a/Localization/Defaults/City.php +++ b/Localization/Defaults/City.php @@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults; * @link https://orange-management.org * @since 1.0.0 */ -final class City +class City { /** * City id. diff --git a/Localization/Defaults/Country.php b/Localization/Defaults/Country.php index a5af97ff7..2feaf1d5f 100644 --- a/Localization/Defaults/Country.php +++ b/Localization/Defaults/Country.php @@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults; * @link https://orange-management.org * @since 1.0.0 */ -final class Country +class Country { /** * Country id. diff --git a/Localization/Defaults/Currency.php b/Localization/Defaults/Currency.php index ee0592a5f..f151303f3 100644 --- a/Localization/Defaults/Currency.php +++ b/Localization/Defaults/Currency.php @@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults; * @link https://orange-management.org * @since 1.0.0 */ -final class Currency +class Currency { /** * Currency id. diff --git a/Localization/Defaults/Iban.php b/Localization/Defaults/Iban.php index 7da04a662..6c3b822aa 100644 --- a/Localization/Defaults/Iban.php +++ b/Localization/Defaults/Iban.php @@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults; * @link https://orange-management.org * @since 1.0.0 */ -final class Iban +class Iban { /** * Iban id. diff --git a/Localization/Defaults/Language.php b/Localization/Defaults/Language.php index 410cd214a..2e17332fe 100644 --- a/Localization/Defaults/Language.php +++ b/Localization/Defaults/Language.php @@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults; * @link https://orange-management.org * @since 1.0.0 */ -final class Language +class Language { /** * Language id. diff --git a/Localization/Defaults/NullCity.php b/Localization/Defaults/NullCity.php new file mode 100644 index 000000000..30d7ea10a --- /dev/null +++ b/Localization/Defaults/NullCity.php @@ -0,0 +1,27 @@ + * @since 1.0.0 */ - private array $datetime = []; + protected array $datetime = []; /** * Weight. @@ -106,7 +106,7 @@ class Localization * @var array * @since 1.0.0 */ - private array $weight = []; + protected array $weight = []; /** * Speed. @@ -114,7 +114,7 @@ class Localization * @var array * @since 1.0.0 */ - private array $speed = []; + protected array $speed = []; /** * Length. @@ -122,7 +122,7 @@ class Localization * @var array * @since 1.0.0 */ - private array $length = []; + protected array $length = []; /** * Area. @@ -130,7 +130,7 @@ class Localization * @var array * @since 1.0.0 */ - private array $area = []; + protected array $area = []; /** * Volume. @@ -138,7 +138,7 @@ class Localization * @var array * @since 1.0.0 */ - private array $volume = []; + protected array $volume = []; /** * Country id. @@ -146,7 +146,7 @@ class Localization * @var int * @since 1.0.0 */ - private $id = 0; + protected int $id = 0; /** * Get id diff --git a/Localization/NullLocalization.php b/Localization/NullLocalization.php new file mode 100644 index 000000000..771c7f454 --- /dev/null +++ b/Localization/NullLocalization.php @@ -0,0 +1,27 @@ +