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
{
$key = $this->model . '_' . $this->primaryId . '_' . $this->value;
return $this->_relation->count($key);
return $this->_relation->count($this->reKey());
}
}
+1 -2
View File
@@ -27,7 +27,6 @@ class HasMany extends HasBase
*/
public function get(): array|Collection|null
{
$key = $this->model . '_' . $this->primaryId . '_' . $this->value;
return $this->_relation->get(md5($key));
return $this->_relation->get($this->reKey());
}
}
+1 -2
View File
@@ -26,7 +26,6 @@ class HasOne extends HasBase
*/
public function get(): array|ModelInterface|null
{
$key = $this->model . '_' . $this->primaryId . '_' . $this->value;
return $this->_relation->first(md5($key));
return $this->_relation->first($this->reKey());
}
}
+10 -15
View File
@@ -6,6 +6,7 @@ namespace Database;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Context;
/**
* Class Relation
@@ -46,16 +47,14 @@ class Relation extends Component
*/
public function first(string $_identification): mixed
{
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
if (Context::hasContext($_identification)) {
return Context::getContext($_identification);
}
$activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
return Context::setContext($_identification, $activeModel);
}
@@ -65,16 +64,14 @@ class Relation extends Component
*/
public function count(string $_identification): mixed
{
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
if (Context::hasContext($_identification)) {
return Context::getContext($_identification);
}
$activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
return Context::setContext($_identification, $activeModel);
}
@@ -84,16 +81,14 @@ class Relation extends Component
*/
public function get(string $_identification): mixed
{
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
if (Context::hasContext($_identification)) {
return Context::getContext($_identification);
}
$activeModel = $this->_query[$_identification]->get();
if (empty($activeModel)) {
return $activeModel;
}
return $this->_relations[$_identification] = $activeModel;
return Context::setContext($_identification, $activeModel);
}
}
+10 -2
View File
@@ -74,8 +74,7 @@ abstract class HasBase implements \Database\Traits\Relation
{
if (!method_exists($this, $name)) {
$key = $this->model . '_' . $this->primaryId . '_' . $this->value;
var_dump($this->model, $this->primaryId, $this->value);
$this->_relation->getQuery(md5($key))->$name(...$arguments);
$this->_relation->getQuery($this->reKey())->$name(...$arguments);
} else {
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
* @return mixed