This commit is contained in:
2022-09-29 18:58:32 +08:00
parent 02eddb98a2
commit 322168ff68
5 changed files with 19 additions and 29 deletions
+2 -1
View File
@@ -34,7 +34,8 @@ class HasCount extends HasBase
*/
public function get(): array|ModelInterface|null
{
return $this->_relation->count($this->model::className(), $this->value);
$key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value;
return $this->_relation->count($key);
}
}
+2 -1
View File
@@ -42,6 +42,7 @@ class HasMany extends HasBase
*/
public function get(): array|Collection|null
{
return $this->_relation->get($this->model::className(), $this->value);
$key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value;
return $this->_relation->get($key);
}
}
+2 -1
View File
@@ -41,6 +41,7 @@ class HasOne extends HasBase
*/
public function get(): array|ModelInterface|null
{
return $this->_relation->first($this->model::className(), $this->value);
$key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value;
return $this->_relation->first($key);
}
}
+9 -21
View File
@@ -41,19 +41,16 @@ class Relation extends Component
/**
* @param string $identification
* @param $localValue
* @param string $_identification
* @return mixed
* @throws Exception
*/
public function first(string $identification, $localValue): mixed
public function first(string $_identification): mixed
{
$_identification = $identification . '_first_' . $localValue;
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->first();
$activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) {
return null;
}
@@ -63,19 +60,16 @@ class Relation extends Component
/**
* @param string $identification
* @param $localValue
* @param string $_identification
* @return mixed
* @throws Exception
*/
public function count(string $identification, $localValue): mixed
public function count(string $_identification): mixed
{
$_identification = $identification . '_count_' . $localValue;
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->count();
$activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) {
return null;
}
@@ -85,22 +79,16 @@ class Relation extends Component
/**
* @param string $identification
* @param $localValue
* @param string $_identification
* @return mixed
*/
public function get(string $identification, $localValue): mixed
public function get(string $_identification): mixed
{
if (is_array($localValue)) {
$_identification = $identification . '_get_' . implode('_', $localValue);
} else {
$_identification = $identification . '_get_' . $localValue;
}
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->get();
$activeModel = $this->_query[$_identification]->get();
if (empty($activeModel)) {
return $activeModel;
}
+4 -5
View File
@@ -40,12 +40,12 @@ abstract class HasBase implements \Database\Traits\Relation
/**
* HasBase constructor.
* @param ModelInterface $model
* @param $primaryId
* @param string $primaryId
* @param $value
* @param Relation $relation
* @throws Exception
*/
public function __construct(mixed $model, $primaryId, $value, Relation $relation)
public function __construct(mixed $model, public string $primaryId, $value, Relation $relation)
{
if (!class_exists($model)) {
throw new Exception('Model must implement ' . $model);
@@ -60,10 +60,9 @@ abstract class HasBase implements \Database\Traits\Relation
$_model = $model::query()->where(['t1.' . $primaryId => $value]);
}
$this->_relation = $relation->bindIdentification($model . '_' . $primaryId . '_' . $value, $_model);
$this->value = is_array($value) ? json_encode($value,JSON_UNESCAPED_UNICODE) : $value;
$this->_relation = $relation->bindIdentification($model . '_' . $primaryId . '_' . $this->value, $_model);
$this->model = $model;
$this->value = $value;
}