This commit is contained in:
2022-09-29 23:06:07 +08:00
parent 76154631e5
commit 77f63a85ed
5 changed files with 23 additions and 23 deletions
+1 -2
View File
@@ -19,8 +19,7 @@ class HasCount extends HasBase
*/ */
public function get(): array|ModelInterface|null public function get(): array|ModelInterface|null
{ {
$key = $this->model . '_' . $this->primaryId . '_' . $this->value; return $this->_relation->count($this->reKey());
return $this->_relation->count($key);
} }
} }
+1 -2
View File
@@ -27,7 +27,6 @@ class HasMany extends HasBase
*/ */
public function get(): array|Collection|null public function get(): array|Collection|null
{ {
$key = $this->model . '_' . $this->primaryId . '_' . $this->value; return $this->_relation->get($this->reKey());
return $this->_relation->get(md5($key));
} }
} }
+1 -2
View File
@@ -26,7 +26,6 @@ class HasOne extends HasBase
*/ */
public function get(): array|ModelInterface|null public function get(): array|ModelInterface|null
{ {
$key = $this->model . '_' . $this->primaryId . '_' . $this->value; return $this->_relation->first($this->reKey());
return $this->_relation->first(md5($key));
} }
} }
+10 -15
View File
@@ -6,6 +6,7 @@ namespace Database;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Context;
/** /**
* Class Relation * Class Relation
@@ -46,16 +47,14 @@ class Relation extends Component
*/ */
public function first(string $_identification): mixed public function first(string $_identification): mixed
{ {
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { if (Context::hasContext($_identification)) {
return $this->_relations[$_identification]; return Context::getContext($_identification);
} }
$activeModel = $this->_query[$_identification]->first(); $activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) { if (empty($activeModel)) {
return null; return null;
} }
return Context::setContext($_identification, $activeModel);
return $this->_relations[$_identification] = $activeModel;
} }
@@ -65,16 +64,14 @@ class Relation extends Component
*/ */
public function count(string $_identification): mixed public function count(string $_identification): mixed
{ {
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { if (Context::hasContext($_identification)) {
return $this->_relations[$_identification]; return Context::getContext($_identification);
} }
$activeModel = $this->_query[$_identification]->count(); $activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) { if (empty($activeModel)) {
return null; return null;
} }
return Context::setContext($_identification, $activeModel);
return $this->_relations[$_identification] = $activeModel;
} }
@@ -84,16 +81,14 @@ class Relation extends Component
*/ */
public function get(string $_identification): mixed public function get(string $_identification): mixed
{ {
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) { if (Context::hasContext($_identification)) {
return $this->_relations[$_identification]; return Context::getContext($_identification);
} }
$activeModel = $this->_query[$_identification]->get(); $activeModel = $this->_query[$_identification]->get();
if (empty($activeModel)) { if (empty($activeModel)) {
return $activeModel; return $activeModel;
} }
return Context::setContext($_identification, $activeModel);
return $this->_relations[$_identification] = $activeModel;
} }
} }
+10 -2
View File
@@ -74,8 +74,7 @@ abstract class HasBase implements \Database\Traits\Relation
{ {
if (!method_exists($this, $name)) { if (!method_exists($this, $name)) {
$key = $this->model . '_' . $this->primaryId . '_' . $this->value; $key = $this->model . '_' . $this->primaryId . '_' . $this->value;
var_dump($this->model, $this->primaryId, $this->value); $this->_relation->getQuery($this->reKey())->$name(...$arguments);
$this->_relation->getQuery(md5($key))->$name(...$arguments);
} else { } else {
call_user_func([$this, $name], ...$arguments); call_user_func([$this, $name], ...$arguments);
} }
@@ -83,6 +82,15 @@ abstract class HasBase implements \Database\Traits\Relation
} }
/**
* @return string
*/
protected function reKey(): string
{
return md5($this->model . '_' . $this->primaryId . '_' . $this->value);
}
/** /**
* @param $name * @param $name
* @return mixed * @return mixed