Files
kiri-databases/Relation.php
T

93 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2022-01-09 03:49:51 +08:00
<?php
declare(strict_types=1);
namespace Database;
2024-11-06 20:53:24 +08:00
use Database\Base\ActiveQueryInterface;
2022-01-09 03:49:51 +08:00
use Kiri\Abstracts\Component;
2022-12-12 17:31:12 +08:00
use Kiri\Di\Context;
2024-11-06 21:33:36 +08:00
use Swoole\Coroutine;
2022-01-09 03:49:51 +08:00
/**
* Class Relation
* @package Kiri\db
*/
class Relation extends Component
{
2023-12-12 15:35:35 +08:00
/** @var ActiveQuery[] $_query */
private array $_query = [];
2022-01-09 03:49:51 +08:00
2023-12-12 15:35:35 +08:00
/**
* @param string $identification
* @param ActiveQuery $query
* @return $this
*/
public function bindIdentification(string $identification, ActiveQuery $query): static
{
$this->_query[$identification] = $query;
return $this;
}
2022-01-09 03:49:51 +08:00
2023-12-12 15:35:35 +08:00
/**
* @param string $identification
* @return bool
*/
public function hasIdentification(string $identification): bool
{
2024-11-06 21:03:11 +08:00
return isset($this->_query[$identification]);
2023-12-12 15:35:35 +08:00
}
2023-04-01 17:11:47 +08:00
2023-12-12 15:35:35 +08:00
/**
* @param string $name
* @return ActiveQuery|null
*/
public function getQuery(string $name): ?ActiveQuery
{
return $this->_query[$name] ?? null;
}
2022-01-09 03:49:51 +08:00
2023-12-12 15:35:35 +08:00
/**
* @param string $_identification
* @return mixed
* @throws
*/
public function first(string $_identification): mixed
{
2024-11-06 21:28:29 +08:00
if (!Context::exists($_identification)) {
2024-11-06 21:41:43 +08:00
Context::set($_identification, $this->_query[$_identification]->first());
2023-12-12 15:35:35 +08:00
}
2024-11-06 21:28:29 +08:00
return Context::get($_identification);
2023-12-12 15:35:35 +08:00
}
2022-01-09 03:49:51 +08:00
2023-12-12 15:35:35 +08:00
/**
* @param string $_identification
* @return mixed
*/
public function count(string $_identification): mixed
{
2024-11-06 21:28:29 +08:00
if (!Context::exists($_identification)) {
2024-11-06 21:41:43 +08:00
Context::set($_identification, $this->_query[$_identification]->count());
2023-12-12 15:35:35 +08:00
}
2024-11-06 21:28:29 +08:00
return Context::get($_identification);
2023-12-12 15:35:35 +08:00
}
2022-01-09 03:49:51 +08:00
2023-07-25 09:33:24 +08:00
/**
* @param string $_identification
* @return mixed
2023-12-12 15:35:35 +08:00
* @throws
2023-07-25 09:33:24 +08:00
*/
2023-12-12 15:35:35 +08:00
public function get(string $_identification): mixed
{
if (Context::exists($_identification)) {
2024-11-06 21:41:43 +08:00
Context::set($_identification, $this->_query[$_identification]->get());
2023-12-12 15:35:35 +08:00
}
2024-11-06 21:28:29 +08:00
return Context::get($_identification);
2023-12-12 15:35:35 +08:00
}
2022-01-09 03:49:51 +08:00
}