Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/Interfaces/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace Ang3\Component\Odoo\Interfaces;

use Ang3\Component\Odoo\DBAL\Expression\DomainInterface;
use Illuminate\Support\Collection;

interface ModelInterface
{
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;

Expand Down
32 changes: 16 additions & 16 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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'));
Expand All @@ -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};
Expand Down Expand Up @@ -202,7 +208,7 @@ private function clearUseVars(): void
$this->options = [];
}

public function get(): array
public function get(): Collection
{
$collection = [];

Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -386,4 +386,4 @@ public function __debugInfo()
{
return $this->toArray();
}
}
}