Files
kiri-databases/Relation.php
T

107 lines
2.4 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
declare(strict_types=1);
namespace Database;
2023-04-10 17:45:29 +08:00
use Exception;
2022-01-09 03:49:51 +08:00
use Kiri\Abstracts\Component;
2022-12-12 17:31:12 +08:00
use Kiri\Di\Context;
2022-01-09 03:49:51 +08:00
/**
* Class Relation
* @package Kiri\db
*/
class Relation extends Component
{
/** @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;
}
2023-04-01 17:11:47 +08:00
/**
* @param string $identification
* @return bool
*/
public function hasIdentification(string $identification): bool
{
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
}
2022-01-09 03:49:51 +08:00
/**
* @param string $name
* @return ActiveQuery|null
*/
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
/**
2022-09-29 18:58:32 +08:00
* @param string $_identification
2022-01-09 03:49:51 +08:00
* @return mixed
2023-04-10 17:45:29 +08:00
* @throws Exception
2022-01-09 03:49:51 +08:00
*/
2022-09-29 18:58:32 +08:00
public function first(string $_identification): mixed
2022-01-09 03:49:51 +08:00
{
2023-04-03 11:08:11 +08:00
if (Context::exists($_identification)) {
return Context::get($_identification);
2022-01-09 03:49:51 +08:00
}
2022-09-29 18:58:32 +08:00
$activeModel = $this->_query[$_identification]->first();
2022-01-09 03:49:51 +08:00
if (empty($activeModel)) {
2023-07-25 09:33:24 +08:00
return $this->_query[$_identification]->mock();
2022-01-09 03:49:51 +08:00
}
2023-04-10 18:56:36 +08:00
unset($this->_query[$_identification]);
2023-04-03 11:08:11 +08:00
return Context::set($_identification, $activeModel);
2022-01-09 03:49:51 +08:00
}
/**
2022-09-29 18:58:32 +08:00
* @param string $_identification
2022-01-09 03:49:51 +08:00
* @return mixed
*/
2022-09-29 18:58:32 +08:00
public function count(string $_identification): mixed
2022-01-09 03:49:51 +08:00
{
2023-04-03 11:08:11 +08:00
if (Context::exists($_identification)) {
return Context::get($_identification);
2022-01-09 03:49:51 +08:00
}
2022-09-29 18:58:32 +08:00
$activeModel = $this->_query[$_identification]->count();
2022-01-09 03:49:51 +08:00
if (empty($activeModel)) {
2023-07-25 09:33:24 +08:00
return $this->_query[$_identification]->mock();
2022-01-09 03:49:51 +08:00
}
2023-04-10 18:56:36 +08:00
unset($this->_query[$_identification]);
2023-04-03 11:08:11 +08:00
return Context::set($_identification, $activeModel);
2022-01-09 03:49:51 +08:00
}
2023-07-25 09:33:24 +08:00
/**
* @param string $_identification
* @return mixed
* @throws Exception
*/
2022-09-29 18:58:32 +08:00
public function get(string $_identification): mixed
2022-01-09 03:49:51 +08:00
{
2023-04-03 11:08:11 +08:00
if (Context::exists($_identification)) {
return Context::get($_identification);
2022-01-09 03:49:51 +08:00
}
2022-09-29 18:58:32 +08:00
$activeModel = $this->_query[$_identification]->get();
2022-01-09 03:49:51 +08:00
if (empty($activeModel)) {
2022-04-28 03:22:53 +08:00
return $activeModel;
2022-01-09 03:49:51 +08:00
}
2023-04-10 18:56:36 +08:00
unset($this->_query[$_identification]);
2023-04-03 11:08:11 +08:00
return Context::set($_identification, $activeModel);
2022-01-09 03:49:51 +08:00
}
}