This commit is contained in:
2023-04-11 17:05:03 +08:00
parent 3b7dd9c709
commit c64e05a81e
2 changed files with 57 additions and 14 deletions
+46 -11
View File
@@ -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;
}
+11 -3
View File
@@ -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;
/**