85 lines
1.6 KiB
PHP
85 lines
1.6 KiB
PHP
<?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;
|
|
use Kiri;
|
|
|
|
/**
|
|
* Class HasBase
|
|
* @package Database
|
|
*
|
|
* @include Query
|
|
*
|
|
* @method first($name)
|
|
* @method all($name)
|
|
* @method count($name)
|
|
*/
|
|
abstract class HasBase implements \Database\Traits\Relation
|
|
{
|
|
|
|
/** @var ModelInterface|Collection */
|
|
protected mixed $data = null;
|
|
|
|
/**
|
|
* @var ModelInterface
|
|
*/
|
|
protected mixed $model;
|
|
|
|
|
|
protected mixed $value = 0;
|
|
|
|
|
|
/**
|
|
* HasBase constructor.
|
|
* @param string $name
|
|
*/
|
|
public function __construct(public string $name)
|
|
{
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $name
|
|
* @param array $arguments
|
|
* @return $this|mixed
|
|
* @throws \Exception
|
|
*/
|
|
public function __call(string $name, array $arguments)
|
|
{
|
|
if ($name !== 'get') {
|
|
$relation = Kiri::getDi()->get(Relation::class);
|
|
$query = $relation->getQuery($this->name);
|
|
if (is_null($query)) {
|
|
throw new \Exception('Unknown relation method: ' . $name);
|
|
}
|
|
$query->$name(...$arguments);
|
|
return $this;
|
|
} else {
|
|
return $this->get();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $name
|
|
* @return mixed
|
|
*/
|
|
public function __get(string $name): mixed
|
|
{
|
|
if ($this->data === null) {
|
|
$this->data = $this->get();
|
|
}
|
|
return $this->data;
|
|
}
|
|
}
|