This commit is contained in:
xl
2024-11-06 21:06:01 +08:00
parent 59c9a2e944
commit 4e59053d5c
7 changed files with 20 additions and 10 deletions
+1 -4
View File
@@ -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);
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+2 -2
View File
@@ -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)) {
+1 -1
View File
@@ -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);
}
+13
View File
@@ -0,0 +1,13 @@
<?php
class Users extends \Database\Model
{
public function hasD()
{
return $this->hasOne(static::class, 'id', 'id')->with([]);
}
}