From 83029d48db58aa3bb64bb414e67b3be85e123f62 Mon Sep 17 00:00:00 2001 From: JeuFore Date: Mon, 21 Aug 2023 15:59:31 +0200 Subject: [PATCH 1/2] feat: added laravel support for relations --- composer.json | 3 ++- composer.lock | 2 +- src/Interfaces/ModelInterface.php | 3 ++- src/Model.php | 36 +++++++++++++++---------------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 14c4c7d..2a19e0f 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "symfony/var-dumper": "^6.2.8", "symfony/phpunit-bridge": "^3.4 || ^4.0 || ^5.0", "symfony/property-info": "^3.4 || ^4.0 || ^5.0", - "symfony/inflector": "^3.4 || ^4.0 || ^5.0" + "symfony/inflector": "^3.4 || ^4.0 || ^5.0", + "vlucas/phpdotenv": "^5.5" } } diff --git a/composer.lock b/composer.lock index 3f73851..2974027 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2e599517102a622dab24b1043037a71d", + "content-hash": "9444817b479fb094f9b4156d694efbf0", "packages": [ { "name": "ang3/php-xmlrpc-client", diff --git a/src/Interfaces/ModelInterface.php b/src/Interfaces/ModelInterface.php index fcb8c6e..7c7758b 100644 --- a/src/Interfaces/ModelInterface.php +++ b/src/Interfaces/ModelInterface.php @@ -3,6 +3,7 @@ namespace Ang3\Component\Odoo\Interfaces; use Ang3\Component\Odoo\DBAL\Expression\DomainInterface; +use Illuminate\Support\Collection; interface ModelInterface { @@ -10,7 +11,7 @@ public function __get($key); public function __set(string $key, mixed $value); - public function get() : array; + public function get(): Collection; public static function where(DomainInterface $condition): self; diff --git a/src/Model.php b/src/Model.php index db4889e..79b957d 100644 --- a/src/Model.php +++ b/src/Model.php @@ -11,12 +11,15 @@ use Ang3\Component\Odoo\DBAL\Expression\ExpressionBuilder; use Ang3\Component\Odoo\DBAL\RecordManager; use Ang3\Component\Odoo\Interfaces\ModelInterface; +use Illuminate\Support\Collection; +use Illuminate\Database\Eloquent\Concerns\HasRelationships; use Illuminate\Foundation\Validation\ValidatesRequests; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; abstract class Model implements UrlRoutable, Arrayable, JsonSerializable, ModelInterface { - use ValidatesRequests; + use ValidatesRequests, + HasRelationships; protected $model; @@ -36,8 +39,6 @@ abstract class Model implements UrlRoutable, Arrayable, JsonSerializable, ModelI private RecordManager $rm; - private string $relationIsLoaded = ''; - public function __construct() { $this->client = new Client(getenv('ODOO_URL'), getenv('ODOO_DATABASE'), getenv('ODOO_USER'), getenv('ODOO_PWD')); @@ -54,8 +55,13 @@ public function __get($key) { if (!isset($this->{$key})) if (method_exists($this, $key)) { - $this->relationIsLoaded = $key; - return $this->{$key}(); + if ($this->relationLoaded($key)) + return $this->getRelation($key); + else { + $value = $this->{$key}(); + $this->setRelation($key, $value); + } + return $value; } else return null; return $this->{$key}; @@ -157,7 +163,7 @@ public function getResult(): array ->getQuery() ->setOptions($this->options) ->addOption('fields', $this->select) - ->addOption('context', ['lang' => request()->lang]) + // ->addOption('context', ['lang' => request()->lang]) ->getResult(); $this->clearUseVars(); @@ -178,7 +184,7 @@ private function getOneOrNullResult(): array | null ->getQuery() ->setOptions($this->options) ->addOption('fields', $this->select) - ->addOption('context', ['lang' => request()->lang]) + // ->addOption('context', ['lang' => request()->lang]) ->getOneOrNullResult(); $this->clearUseVars(); @@ -202,7 +208,7 @@ private function clearUseVars(): void $this->options = []; } - public function get(): array + public function get(): Collection { $collection = []; @@ -212,7 +218,7 @@ public function get(): array $model->fillable = []; $collection[] = $model->addAttributes($d); } - return $collection; + return new Collection($collection); } public static function where(DomainInterface $condition): self @@ -286,29 +292,23 @@ public function findBy(string|int $field, $value): self public function hasMany($related, $foreignKey = null, $localKey = null) { - $relations = $this->{$related}; + $relations = collect(); if (is_null($relations)) { $model = new $related(); $foreignKey = $foreignKey ?? str_replace('.', '_', $model->model) . '_id'; $localKey = is_array($this->{$localKey}) ? $this->{$localKey}[0] : $this->{$localKey} ?? $this->id; $relations = $related::where((new ExpressionBuilder())->eq($foreignKey, $localKey))->get(); } - $this->{$related} = $relations; - return $this->{$related}; + return $relations; } public function belongsTo($related, $foreignKey = null, $localKey = null) { - $relationIsLoaded = $this->relationIsLoaded; - $this->relationIsLoaded = ''; - $model = new $related(); $foreignKey = $foreignKey ?? str_replace('.', '_', $model->model) . '_id'; $localKey = is_array($this->{$localKey}) ? $this->{$localKey}[0] : $this->{$localKey} ?? $this->id; $relation = $model->findBy($foreignKey, $localKey); - if ($relationIsLoaded) - $this->{$relationIsLoaded} = $relation; return $relation; } @@ -386,4 +386,4 @@ public function __debugInfo() { return $this->toArray(); } -} \ No newline at end of file +} From 3b180216aa3bbf745515a68a44b9617443271190 Mon Sep 17 00:00:00 2001 From: JeuFore Date: Mon, 21 Aug 2023 16:00:40 +0200 Subject: [PATCH 2/2] fix: uncomment lang --- src/Model.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model.php b/src/Model.php index 79b957d..b44dd51 100644 --- a/src/Model.php +++ b/src/Model.php @@ -163,7 +163,7 @@ public function getResult(): array ->getQuery() ->setOptions($this->options) ->addOption('fields', $this->select) - // ->addOption('context', ['lang' => request()->lang]) + ->addOption('context', ['lang' => request()->lang]) ->getResult(); $this->clearUseVars(); @@ -184,7 +184,7 @@ private function getOneOrNullResult(): array | null ->getQuery() ->setOptions($this->options) ->addOption('fields', $this->select) - // ->addOption('context', ['lang' => request()->lang]) + ->addOption('context', ['lang' => request()->lang]) ->getOneOrNullResult(); $this->clearUseVars();