Files
2025-12-16 20:20:08 +08:00

83 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;
/**
* 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') {
$query = di(Relation::class)->getQuery($this->name);
if (is_null($query)) {
throw new \Exception('Unknown relation key: ' . $this->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;
}
}