变更
This commit is contained in:
+46
-11
@@ -14,8 +14,8 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a
|
|||||||
|
|
||||||
|
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
use Closure;
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
|
use Database\Collection;
|
||||||
use Database\Connection;
|
use Database\Connection;
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Database\Mysql\Columns;
|
use Database\Mysql\Columns;
|
||||||
@@ -24,11 +24,9 @@ use Database\SqlBuilder;
|
|||||||
use Database\Traits\HasBase;
|
use Database\Traits\HasBase;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Annotation\Inject;
|
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Annotation\Annotation;
|
use Kiri\Annotation\Annotation;
|
||||||
use Kiri\Error\StdoutLoggerInterface;
|
use Kiri\Error\StdoutLoggerInterface;
|
||||||
use Kiri\Exception\NotFindClassException;
|
|
||||||
use ReturnTypeWillChange;
|
use ReturnTypeWillChange;
|
||||||
use Kiri\ToArray;
|
use Kiri\ToArray;
|
||||||
use ReflectionException;
|
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
|
* @param null $db
|
||||||
* @return Model|null
|
* @return Model|null
|
||||||
* @throws Exception
|
* @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
|
* @return static|null
|
||||||
* @throws Exception
|
* @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
|
public static function query(): ActiveQuery
|
||||||
{
|
{
|
||||||
$model = new static();
|
$model = new ActiveQuery(static::makeNewInstance());
|
||||||
return (new ActiveQuery($model))->from($model->getTable())->alias('t1');
|
$model->from($model->getTable())->alias('t1');
|
||||||
|
return $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+11
-3
@@ -18,11 +18,19 @@ interface ModelInterface
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|string $param
|
* @param array|string|int $param
|
||||||
* @param null $db
|
* @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;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user