Skip to content

Commit 3410704

Browse files
author
ethan
committed
findOneOrFail
1 parent 6e044f1 commit 3410704

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Builder.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ public function findOne($condition = '', $params = array())
371371
return $arr[0];
372372
}
373373

374+
public function findOneOrFail($condition = '', $params = array())
375+
{
376+
$data = static::findOne($condition, $params);
377+
if ($data == null) {
378+
throw new NotFoundException('Data not found');
379+
}
380+
return $data;
381+
}
382+
374383
/**
375384
* 根据主键查询, 没有结果时返回null
376385
*
@@ -398,7 +407,7 @@ public function findByPkOrFail($id, $primaryKeyField = null)
398407
{
399408
$data = static::findByPk($id, $primaryKeyField);
400409
if ($data == null) {
401-
throw new NotFoundException("Data not found: #" . $id);
410+
throw new NotFoundException('Data not found: #' . $id);
402411
}
403412
return $data;
404413
}

0 commit comments

Comments
 (0)