Files
kiri-databases/Relation.php
T

97 lines
2.4 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
declare(strict_types=1);
namespace Database;
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
{
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
{
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
}
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
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->first();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
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
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->count();
unset($this->_query[$_identification]);
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
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)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->get();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
2022-01-09 03:49:51 +08:00
}