From 4e59053d5cc1cdbbbb09fc44424445980747561b Mon Sep 17 00:00:00 2001 From: xl Date: Wed, 6 Nov 2024 21:06:01 +0800 Subject: [PATCH] eee --- Base/Model.php | 5 +---- HasCount.php | 2 +- HasMany.php | 2 +- HasOne.php | 2 +- Model.php | 4 ++-- Traits/HasBase.php | 2 +- test.php | 13 +++++++++++++ 7 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 test.php diff --git a/Base/Model.php b/Base/Model.php index e8f3279..b5c0c4b 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -598,10 +598,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \ */ public function getRelation(): ?Relation { - if (Context::exists(Relation::class)) { - return Context::get(Relation::class); - } - return Context::set(Relation::class, new Relation()); + return di(Relation::class); } diff --git a/HasCount.php b/HasCount.php index eb58b46..0ede302 100644 --- a/HasCount.php +++ b/HasCount.php @@ -20,7 +20,7 @@ class HasCount extends HasBase */ public function get(): array|ModelInterface|null { - return Context::get(Relation::class)->get($this->name); + return di(Relation::class)->get($this->name); } } diff --git a/HasMany.php b/HasMany.php index 9c15b6f..6839956 100644 --- a/HasMany.php +++ b/HasMany.php @@ -29,6 +29,6 @@ class HasMany extends HasBase */ public function get(): array|Collection|null { - return Context::get(Relation::class)->get($this->name); + return di(Relation::class)->get($this->name); } } diff --git a/HasOne.php b/HasOne.php index 2138e88..3033cf2 100644 --- a/HasOne.php +++ b/HasOne.php @@ -28,6 +28,6 @@ class HasOne extends HasBase */ public function get(): array|ModelInterface|null { - return Context::get(Relation::class)->first($this->name); + return di(Relation::class)->first($this->name); } } diff --git a/Model.php b/Model.php index 1b19a05..6e8cb21 100644 --- a/Model.php +++ b/Model.php @@ -278,7 +278,7 @@ class Model extends Base\Model throw new Exception("Need join table primary key."); } - $relation = $this->getRelation(); + $relation = di(Relation::class); $primaryKey = str_replace('\\\\', '_', $modelName) . '_' . $foreignKey . '_' . $value; if (!$relation->hasIdentification($primaryKey)) { @@ -339,7 +339,7 @@ class Model extends Base\Model throw new Exception("Need join table primary key."); } - $relation = $this->getRelation(); + $relation = di(Relation::class); $primaryKey = str_replace('\\\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value); if (!$relation->hasIdentification($primaryKey)) { diff --git a/Traits/HasBase.php b/Traits/HasBase.php index 21c6683..5b9c79e 100644 --- a/Traits/HasBase.php +++ b/Traits/HasBase.php @@ -58,7 +58,7 @@ abstract class HasBase implements \Database\Traits\Relation public function __call(string $name, array $arguments) { if ($name !== 'get') { - $query = Context::get(Relation::class)->getQuery($this->name); + $query = di(Relation::class)->getQuery($this->name); if (is_null($query)) { throw new \Exception('Unknown relation key: ' . $this->name); } diff --git a/test.php b/test.php new file mode 100644 index 0000000..ee38545 --- /dev/null +++ b/test.php @@ -0,0 +1,13 @@ +hasOne(static::class, 'id', 'id')->with([]); + } +} \ No newline at end of file