This commit is contained in:
xl
2024-10-23 14:36:55 +08:00
parent eb39acb7e9
commit 59375e567e
+6 -4
View File
@@ -237,9 +237,10 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/ */
public static function findOne(int|string|array|null $param): ?static public static function findOne(int|string|array|null $param): ?static
{ {
if (empty($param)) return null; if (empty($param)) {
$model = static::instance(); return null;
$query = new ActiveQuery($model); }
$query = new ActiveQuery($model = static::instance());
$query->from($model->getTable())->alias('t1'); $query->from($model->getTable())->alias('t1');
if (is_numeric($param)) { if (is_numeric($param)) {
$query->where([$model->getPrimary() => +$param]); $query->where([$model->getPrimary() => +$param]);
@@ -250,8 +251,9 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
} }
if (($data = $query->first()) === false) { if (($data = $query->first()) === false) {
throw new Exception($model->getLastError()); throw new Exception($model->getLastError());
} else {
return $data;
} }
return $data;
} }