Db::getIdObject
Usage
?object Db::getIdObject(string $class_name, string $table_name, string | int $id, [ string $id_col = '' ])Description
Get a single row by the value of the table's id / primary key column mapped to an object.Parameters
Parameter | Required | Type | Description |
---|---|---|---|
$class_name | Yes | string | The fully qualified class name to map the database record to. |
$table_name | Yes | string | The table name to retrieve record from. |
$id | Yes | string | int | The value of the id / primary key column to lookup. |
$id_col | No | string | The column to search in. Defaults to the primary key column of the table. |
Return Value
Returns an object with its properties mapped to an instance of the provided class name of the record retrieved, or null if no record found.Examples
Get Object By ID
use App\Demo\Models\Product;
$id = 81;
if (!$product = $this->db->getIdObject(Product::class, 'products', $id)) {
die("No product found with id# $id");
}
echo "Got object: " . $product::class . "\n";