From c64e05a81e0a6f4aebad17b0e2057936e35bb9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 11 Apr 2023 17:05:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Base/Model.php | 57 +++++++++++++++++++++++++++++++++++++--------- ModelInterface.php | 14 +++++++++--- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/Base/Model.php b/Base/Model.php index 33486c4..64a9b25 100644 --- a/Base/Model.php +++ b/Base/Model.php @@ -14,8 +14,8 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a use ArrayAccess; -use Closure; use Database\ActiveQuery; +use Database\Collection; use Database\Connection; use Database\ModelInterface; use Database\Mysql\Columns; @@ -24,11 +24,9 @@ use Database\SqlBuilder; use Database\Traits\HasBase; use Exception; use Kiri; -use Kiri\Annotation\Inject; use Kiri\Abstracts\Component; use Kiri\Annotation\Annotation; use Kiri\Error\StdoutLoggerInterface; -use Kiri\Exception\NotFindClassException; use ReturnTypeWillChange; use Kiri\ToArray; use ReflectionException; @@ -237,14 +235,36 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T } /** - * @param array|string $param + * @param int|string|array $param * @param null $db * @return Model|null * @throws Exception */ - public static function findOne(array|string $param, $db = NULL): static|null + public static function findOne(int|string|array $param, $db = NULL): ?static { - return static::query()->where($param)->first(); + $model = new ActiveQuery(static::makeNewInstance()); + $model->from($model->getTable())->alias('t1'); + if (is_numeric($param)) { + $model->where([$model->modelClass->getPrimary() => $param]); + } else { + $model->where($param); + } + return $model->first(); + } + + + /** + * @param int $param + * @param null $db + * @return Model|null + * @throws Exception + */ + public static function primary(int $param, $db = NULL): ?static + { + $model = new ActiveQuery(static::makeNewInstance()); + $model->from($model->getTable())->alias('t1'); + $model->where([$model->modelClass->getPrimary() => $param]); + return $model->first(); } @@ -258,13 +278,27 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T /** - * @param $condition + * @param int|string|array $condition * @return static|null * @throws Exception */ - public static function first($condition): ?static + public static function first(int|string|array $condition): ?static { - return static::query()->where($condition)->first(); + return static::findOne($condition); + } + + + /** + * @param string|array $condition + * @return Collection + * @throws Exception + */ + public static function all(string|array $condition): Collection + { + $model = new ActiveQuery(static::makeNewInstance()); + $model->from($model->getTable())->alias('t1'); + $model->where($condition); + return $model->get(); } @@ -274,8 +308,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, T */ public static function query(): ActiveQuery { - $model = new static(); - return (new ActiveQuery($model))->from($model->getTable())->alias('t1'); + $model = new ActiveQuery(static::makeNewInstance()); + $model->from($model->getTable())->alias('t1'); + return $model; } diff --git a/ModelInterface.php b/ModelInterface.php index 88d5df2..671d2f4 100644 --- a/ModelInterface.php +++ b/ModelInterface.php @@ -18,11 +18,19 @@ interface ModelInterface { /** - * @param array|string $param + * @param array|string|int $param * @param null $db - * @return ModelInterface + * @return ModelInterface|null */ - public static function findOne(array|string $param, $db = NULL): mixed; + public static function findOne(array|string|int $param, $db = NULL): ?static; + + + /** + * @param int $param + * @param null $db + * @return ModelInterface|null + */ + public static function primary(int $param, $db = NULL): ?static; /**