Explorar el Código

Add model require method

Alan Lam hace 3 años
padre
commit
50d3d0e92e
Se han modificado 1 ficheros con 14 adiciones y 0 borrados
  1. 14 0
      engine/Model.php

+ 14 - 0
engine/Model.php

@@ -167,6 +167,20 @@ abstract class Model {
 		return $object;
 		return $object;
 	}
 	}
 
 
+	/**
+	 * @param ...$id_parts
+	 * @return static
+	 * @throws Exception
+	 */
+	public static function require(...$id_parts): object {
+		$row = static::get(...$id_parts);
+		if(!$row) {
+			$name = ucwords(str_replace("_", " ", static::$table));
+			throw new RuntimeException(__("%s not found.", $name));
+		}
+		return $row;
+	}
+
 	/**
 	/**
 	 * @return static[]
 	 * @return static[]
 	 * @throws Exception
 	 * @throws Exception