This commit is contained in:
2023-12-12 15:35:35 +08:00
parent 510a13e3da
commit e2c0f62532
32 changed files with 922 additions and 1095 deletions
+69 -79
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Database;
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Di\Context;
@@ -15,92 +14,83 @@ use Kiri\Di\Context;
class Relation extends Component
{
/** @var ActiveQuery[] $_query */
private array $_query = [];
/** @var ActiveQuery[] $_query */
private array $_query = [];
/**
* @param string $identification
* @param ActiveQuery $query
* @return $this
*/
public function bindIdentification(string $identification, ActiveQuery $query): static
{
$this->_query[$identification] = $query;
return $this;
}
/**
* @param string $identification
* @param ActiveQuery $query
* @return $this
*/
public function bindIdentification(string $identification, ActiveQuery $query): static
{
$this->_query[$identification] = $query;
return $this;
}
/**
* @param string $identification
* @return bool
*/
public function hasIdentification(string $identification): bool
{
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
}
/**
* @param string $identification
* @return bool
*/
public function hasIdentification(string $identification): bool
{
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
}
/**
* @param string $name
* @return ActiveQuery|null
*/
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
/**
* @param string $_identification
* @return mixed
* @throws Exception
*/
public function first(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) {
return $this->_query[$_identification]->mock();
}
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/**
* @param string $_identification
* @return mixed
*/
public function count(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) {
return $this->_query[$_identification]->mock();
}
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/**
* @param string $name
* @return ActiveQuery|null
*/
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
/**
* @param string $_identification
* @return mixed
* @throws Exception
* @throws
*/
public function get(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->get();
if (empty($activeModel)) {
return $activeModel;
}
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
public function first(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->first();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/**
* @param string $_identification
* @return mixed
*/
public function count(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->count();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/**
* @param string $_identification
* @return mixed
* @throws
*/
public function get(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->get();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
}