CustomerMailingList
extends Model
in package
Model for managing customer mailing list subscriptions per business.
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.
- countForBusiness() : int
- Count subscribers for a business.
- create() : int
- Create a new record.
- delete() : bool
- Delete a record by primary key.
- exportForBusiness() : array<string|int, mixed>
- Export all subscribers for a business as an array.
- 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.
- findByBusinessAndEmail() : array<string|int, mixed>|null
- Find a mailing list entry by business and email.
- getDb() : PDO
- Get the underlying PDO connection.
- getForBusiness() : array<string|int, mixed>
- Get all mailing list entries for a business.
- isSubscribed() : bool
- Check if an email is subscribed to a business mailing list.
- paginate() : array<string|int, mixed>
- Paginate records matching conditions.
- query() : PDOStatement
- Execute a raw SQL query with parameter binding.
- search() : array<string|int, mixed>
- Search mailing list by name or email.
- subscribe() : int|bool
- Subscribe an email to a business mailing list.
- unsubscribe() : bool
- Unsubscribe an email from a business mailing list.
- 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', 'customer_id', 'email', 'name', 'status', 'subscribed_at', 'unsubscribed_at']
Mass-assignable fields
$primaryKey
protected
string
$primaryKey
= 'id'
Primary key column
$table
protected
string
$table
= 'customer_mailing_lists'
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
intcountForBusiness()
Count subscribers for a business.
public
countForBusiness(int $businessId[, string $status = 'subscribed' ]) : int
Parameters
- $businessId : int
-
Business ID
- $status : string = 'subscribed'
-
Status filter
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
boolexportForBusiness()
Export all subscribers for a business as an array.
public
exportForBusiness(int $businessId) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
Return values
array<string|int, mixed>find()
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>|nullfindByBusinessAndEmail()
Find a mailing list entry by business and email.
public
findByBusinessAndEmail(int $businessId, string $email) : array<string|int, mixed>|null
Parameters
- $businessId : int
-
Business ID
- $email : string
-
Email address
Return values
array<string|int, mixed>|nullgetDb()
Get the underlying PDO connection.
public
getDb() : PDO
Return values
PDOgetForBusiness()
Get all mailing list entries for a business.
public
getForBusiness(int $businessId[, string $status = 'subscribed' ][, int $limit = 500 ]) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $status : string = 'subscribed'
-
Optional status filter ('subscribed', 'unsubscribed')
- $limit : int = 500
-
Max results
Return values
array<string|int, mixed>isSubscribed()
Check if an email is subscribed to a business mailing list.
public
isSubscribed(int $businessId, string $email) : bool
Parameters
- $businessId : int
-
Business ID
- $email : string
-
Email address
Return values
boolpaginate()
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
PDOStatementsearch()
Search mailing list by name or email.
public
search(int $businessId, string $query) : array<string|int, mixed>
Parameters
- $businessId : int
-
Business ID
- $query : string
-
Search query
Return values
array<string|int, mixed>subscribe()
Subscribe an email to a business mailing list.
public
subscribe(int $businessId, string $email[, string|null $name = null ][, int|null $customerId = null ]) : int|bool
Parameters
- $businessId : int
-
Business ID
- $email : string
-
Email address
- $name : string|null = null
-
Subscriber name
- $customerId : int|null = null
-
Customer ID (if logged in)
Return values
int|bool —New record ID or true if resubscribed
unsubscribe()
Unsubscribe an email from a business mailing list.
public
unsubscribe(int $businessId, string $email) : bool
Parameters
- $businessId : int
-
Business ID
- $email : string
-
Email address
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