Files
kiri-databases/Traits/HasBase.php
T

80 lines
1.3 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 15:47
*/
declare(strict_types=1);
namespace Database\Traits;
use Database\ModelInterface;
use Database\Collection;
use Database\Relation;
2023-04-01 17:11:47 +08:00
use Kiri;
2022-01-09 03:49:51 +08:00
/**
* Class HasBase
* @package Database
*
* @include Query
2023-04-01 17:11:47 +08:00
*
* @method first($name)
* @method all($name)
* @method count($name)
2022-01-09 03:49:51 +08:00
*/
abstract class HasBase implements \Database\Traits\Relation
{
2022-09-29 22:51:41 +08:00
2022-01-09 03:49:51 +08:00
/** @var ModelInterface|Collection */
2023-04-01 18:50:44 +08:00
protected mixed $data = null;
2022-09-29 22:51:41 +08:00
2022-01-09 03:49:51 +08:00
/**
* @var ModelInterface
*/
protected mixed $model;
2023-04-01 21:04:28 +08:00
2022-09-29 22:51:41 +08:00
protected mixed $value = 0;
2023-04-01 21:04:28 +08:00
2022-09-29 18:54:14 +08:00
/**
* HasBase constructor.
2023-04-01 17:11:47 +08:00
* @param string $name
2022-09-29 18:54:14 +08:00
*/
2023-04-01 17:11:47 +08:00
public function __construct(public string $name)
2022-01-09 03:49:51 +08:00
{
}
2023-04-20 23:16:36 +08:00
2022-09-29 22:51:41 +08:00
/**
* @param $name
* @param $arguments
* @return static
2023-04-20 23:16:36 +08:00
* @throws \ReflectionException
2022-09-29 22:51:41 +08:00
*/
public function __call($name, $arguments)
{
2023-04-01 21:04:28 +08:00
if ($name !== 'get') {
2023-04-20 23:16:36 +08:00
$relation = Kiri::getDi()->get(Relation::class);
$relation->getQuery($this->name)->$name(...$arguments);
return $this;
2022-09-29 22:51:41 +08:00
} else {
2023-04-01 21:04:28 +08:00
return $this->get();
2022-09-29 22:51:41 +08:00
}
}
2023-04-01 21:04:28 +08:00
2022-01-09 03:49:51 +08:00
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
2023-04-01 18:50:44 +08:00
if ($this->data === null) {
$this->data = $this->get();
}
return $this->data;
2022-01-09 03:49:51 +08:00
}
}