Db::fetchObject
Usage
?object Db::fetchObject(PDOStatement $stmt, string $class_name, [ ?int $position = null ])Description
Get the next row of a query result mapped to an object.Parameters
Parameter | Required | Type | Description |
---|---|---|---|
$stmt | Yes | PDOStatement | The query result returned by the query() method. |
$class_name | Yes | string | The fully qualified class name of the to map the database record to. |
$position | No | ?int |
Return Value
An object with its properties mapped to the next record within the result set, or null if no record exists.Examples
Fetch Array
use App\Demo\Models\Product;
$cat_id = 59;
$result = $this->db->query("SELECT * FROM products WHERE category_id = %i ORDER BY name", $cat_id);
while ($obj = $this->db->fetchObject($result, Product::class)) {
echo "Class: " . $obj::class . "\n";
}