Photo
extends Model
in package
Photo model for managing uploaded images (menu items, gallery, banners, etc.).
Tags
Table of Contents
Properties
- $db : PDO
- $fillable : array<string|int, mixed>
- $primaryKey : string
- $table : string
Methods
- __construct() : mixed
- Initialize model with database connection.
- all() : array<string|int, mixed>
- Get all records from the table.
- count() : int
- Count records matching optional conditions.
- create() : int
- Create a new record.
- delete() : bool
- Delete a record by primary key.
- deleteWithFile() : bool
- Delete a photo record and its associated files.
- find() : array<string|int, mixed>|null
- Find a record by primary key.
- findBy() : array<string|int, mixed>|null
- Find a record by a specific field value.
- getDb() : PDO
- Get the underlying PDO connection.
- getForBusiness() : array<string|int, mixed>
- Get all photos for a business, optionally filtered by type.
- getForItem() : array<string|int, mixed>
- Get all photos for a menu item.
- getPrimaryForItem() : array<string|int, mixed>|null
- Get the primary photo for a menu item.
- getPrimaryForItems() : array<string|int, mixed>
- Get primary photos for multiple menu items in one query.
- paginate() : array<string|int, mixed>
- Paginate records matching conditions.
- query() : PDOStatement
- Execute a raw SQL query with parameter binding.
- setPrimary() : bool
- Set a photo as primary for its menu item, unsetting others.
- update() : bool
- Update a record by primary key.
- where() : array<string|int, mixed>
- Find records matching conditions.
- filterFillable() : array<string|int, mixed>
- Filter data to only include fillable fields.
Properties
$db
protected
PDO
$db
Database connection
$fillable
protected
array<string|int, mixed>
$fillable
= ['business_id', 'menu_item_id', 'photo_type', 'file_name', 'file_path', 'thumbnail_path', 'original_name', 'mime_type', 'file_size', 'width', 'height', 'alt_text', 'caption', 'sort_order', 'is_primary']
Mass-assignable fields
$primaryKey
protected
string
$primaryKey
= 'id'
Primary key column
$table
protected
string
$table
= 'photos'
Database table name
Methods
__construct()
Initialize model with database connection.
public
__construct() : mixed
all()
Get all records from the table.
public
all([string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction (ASC or DESC)
Return values
array<string|int, mixed>count()
Count records matching optional conditions.
public
count([array<string|int, mixed> $conditions = [] ]) : int
Parameters
- $conditions : array<string|int, mixed> = []
-
Associative array of field => value pairs
Return values
intcreate()
Create a new record.
public
create(array<string|int, mixed> $data) : int
Parameters
- $data : array<string|int, mixed>
-
Associative array of field => value pairs
Return values
int —The ID of the newly created record
delete()
Delete a record by primary key.
public
delete(int $id) : bool
Parameters
- $id : int
-
Primary key value
Return values
booldeleteWithFile()
Delete a photo record and its associated files.
public
deleteWithFile(int $id) : bool
Parameters
- $id : int
-
Photo ID
Return values
boolfind()
Find a record by primary key.
public
find(int $id) : array<string|int, mixed>|null
Parameters
- $id : int
-
Primary key value
Return values
array<string|int, mixed>|nullfindBy()
Find a record by a specific field value.
public
findBy(string $field, mixed $value) : array<string|int, mixed>|null
Parameters
- $field : string
-
Column name
- $value : mixed
-
Value to match
Return values
array<string|int, mixed>|nullgetDb()
Get the underlying PDO connection.
public
getDb() : PDO
Return values
PDOgetForBusiness()
Get all photos for a business, optionally filtered by type.
public
getForBusiness(int $businessId[, string|null $type = null ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $type : string|null = null
-
Photo type filter
Return values
array<string|int, mixed>getForItem()
Get all photos for a menu item.
public
getForItem(int $menuItemId) : array<string|int, mixed>
Parameters
- $menuItemId : int
-
Menu item ID
Return values
array<string|int, mixed>getPrimaryForItem()
Get the primary photo for a menu item.
public
getPrimaryForItem(int $menuItemId) : array<string|int, mixed>|null
Parameters
- $menuItemId : int
-
Menu item ID
Return values
array<string|int, mixed>|nullgetPrimaryForItems()
Get primary photos for multiple menu items in one query.
public
getPrimaryForItems(array<string|int, mixed> $menuItemIds) : array<string|int, mixed>
Parameters
- $menuItemIds : array<string|int, mixed>
-
Array of menu item IDs
Return values
array<string|int, mixed> —Associative array keyed by menu_item_id
paginate()
Paginate records matching conditions.
public
paginate(array<string|int, mixed> $conditions, int $page, int $perPage[, string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $conditions : array<string|int, mixed>
-
Associative array of field => value pairs
- $page : int
-
Current page number (1-based)
- $perPage : int
-
Records per page
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction (ASC or DESC)
Return values
array<string|int, mixed> —Pagination result with data, total, page, per_page, total_pages
query()
Execute a raw SQL query with parameter binding.
public
query(string $sql[, array<string|int, mixed> $params = [] ]) : PDOStatement
Parameters
- $sql : string
-
SQL query string
- $params : array<string|int, mixed> = []
-
Bound parameters
Return values
PDOStatementsetPrimary()
Set a photo as primary for its menu item, unsetting others.
public
setPrimary(int $photoId, int $menuItemId) : bool
Parameters
- $photoId : int
-
Photo ID
- $menuItemId : int
-
Menu item ID
Return values
boolupdate()
Update a record by primary key.
public
update(int $id, array<string|int, mixed> $data) : bool
Parameters
- $id : int
-
Primary key value
- $data : array<string|int, mixed>
-
Associative array of field => value pairs
Return values
boolwhere()
Find records matching conditions.
public
where(array<string|int, mixed> $conditions[, string $orderBy = 'id' ][, string $direction = 'ASC' ]) : array<string|int, mixed>
Parameters
- $conditions : array<string|int, mixed>
-
Associative array of field => value pairs
- $orderBy : string = 'id'
-
Column to order by
- $direction : string = 'ASC'
-
Sort direction
Return values
array<string|int, mixed>filterFillable()
Filter data to only include fillable fields.
protected
filterFillable(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
Input data
Return values
array<string|int, mixed> —Filtered data