Files
kiri-core/Database/Relation.php
T

112 lines
2.4 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace Database;
2020-12-17 14:09:14 +08:00
use Exception;
2020-08-31 12:38:32 +08:00
use Snowflake\Abstracts\Component;
/**
* Class Relation
2020-09-07 17:12:46 +08:00
* @package Snowflake\db
2020-08-31 12:38:32 +08:00
*/
class Relation extends Component
{
2020-10-29 18:17:25 +08:00
private array $_relations = [];
2020-08-31 12:38:32 +08:00
/** @var ActiveQuery[] $_query */
2020-10-29 18:17:25 +08:00
private array $_query = [];
2020-08-31 12:38:32 +08:00
/**
* @param string $identification
* @param ActiveQuery $query
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function bindIdentification(string $identification, ActiveQuery $query): static
2020-08-31 12:38:32 +08:00
{
$this->_query[$identification] = $query;
return $this;
}
/**
* @param $name
* @return ActiveQuery|null
*/
2020-12-17 14:09:14 +08:00
public function getQuery(string $name): ?ActiveQuery
2020-08-31 12:38:32 +08:00
{
return $this->_query[$name] ?? null;
}
/**
2020-12-17 14:09:14 +08:00
* @param string $identification
2020-08-31 12:38:32 +08:00
* @param $localValue
2020-12-17 14:09:14 +08:00
* @return mixed
2021-02-23 17:30:10 +08:00
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function first(string $identification, $localValue): mixed
2020-08-31 12:38:32 +08:00
{
2020-11-23 14:22:28 +08:00
$_identification = $identification . '_first_' . $localValue;
2020-08-31 12:38:32 +08:00
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->first();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
}
/**
2020-12-17 14:09:14 +08:00
* @param string $identification
2020-08-31 12:38:32 +08:00
* @param $localValue
2020-12-17 14:09:14 +08:00
* @return mixed
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function count(string $identification, $localValue): mixed
2020-08-31 12:38:32 +08:00
{
2020-11-23 14:22:28 +08:00
$_identification = $identification . '_count_' . $localValue;
2020-08-31 12:38:32 +08:00
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->count();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
}
/**
2020-12-17 14:09:14 +08:00
* @param string $identification
2020-08-31 12:38:32 +08:00
* @param $localValue
2020-12-17 14:09:14 +08:00
* @return mixed
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function get(string $identification, $localValue): mixed
2020-08-31 12:38:32 +08:00
{
if (is_array($localValue)) {
2020-11-23 14:22:28 +08:00
$_identification = $identification . '_get_' . implode('_', $localValue);
2020-08-31 12:38:32 +08:00
} else {
2020-11-23 14:22:28 +08:00
$_identification = $identification . '_get_' . $localValue;
2020-08-31 12:38:32 +08:00
}
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
return $this->_relations[$_identification];
}
$activeModel = $this->_query[$identification]->get();
if (empty($activeModel)) {
return null;
}
return $this->_relations[$_identification] = $activeModel;
}
}