2022-01-09 03:49:51 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/4/4 0004
|
|
|
|
|
* Time: 13:47
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Database;
|
|
|
|
|
|
|
|
|
|
use Database\Traits\HasBase;
|
2022-01-19 14:57:14 +08:00
|
|
|
use Exception;
|
2022-01-09 03:49:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class HasOne
|
|
|
|
|
* @package Database
|
|
|
|
|
* @internal Query
|
|
|
|
|
*/
|
|
|
|
|
class HasOne extends HasBase
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $arguments
|
2022-04-28 03:15:44 +08:00
|
|
|
* @return static
|
2022-01-09 03:49:51 +08:00
|
|
|
*/
|
2022-01-19 14:57:14 +08:00
|
|
|
public function __call($name, $arguments)
|
2022-01-09 03:49:51 +08:00
|
|
|
{
|
2022-01-19 14:59:46 +08:00
|
|
|
if (!method_exists($this, $name)) {
|
2022-09-29 22:38:25 +08:00
|
|
|
$key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value;
|
|
|
|
|
$this->_relation->getQuery($key)->$name(...$arguments);
|
2022-04-28 03:15:44 +08:00
|
|
|
} else {
|
|
|
|
|
call_user_func([$this, $name], ...$arguments);
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array|null|ModelInterface
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function get(): array|ModelInterface|null
|
|
|
|
|
{
|
2022-09-29 18:58:32 +08:00
|
|
|
$key = $this->model::className() . '_' . $this->primaryId . '_' . $this->value;
|
|
|
|
|
return $this->_relation->first($key);
|
2022-01-09 03:49:51 +08:00
|
|
|
}
|
|
|
|
|
}
|