Documentation

CommonModel
in package

Table of Contents

Properties

$db  : mixed

Methods

__construct()  : mixed
count()  : int
Counts the number of records in the specified table that match the given conditions.
create()  : int
Inserts a new record into the specified table and returns the insert ID.
createMany()  : mixed
Inserts multiple records into the specified table.
edit()  : bool
Updates records in the specified table based on the given conditions.
isHave()  : int
Checks if there are any records in the specified table that match the given conditions.
lists()  : array<string|int, mixed>|null|false
notWhereInList()  : object
Retrieves records from the specified table, excluding those where the given key matches any value in the specified array, with optional joins and sorting.
remove()  : bool
Deletes records from the specified table based on the given conditions.
research()  : object
Retrieves records from the specified table that match the given conditions and like patterns.
selectOne()  : object|null
Selects a single record from the database based on conditions.
truncateTable()  : bool
Truncates the specified table, removing all records while keeping the table structure intact.
whereInCheckData()  : int
Checks if there are records in the table where a specified column's value matches any of the provided values.
whereWithJoins()  : object
Retrieves records from the specified table with optional joins, where conditions, like conditions, and ordering, with pagination support.

Properties

Methods

__construct()

public __construct([string $group = 'default' ]) : mixed
Parameters
$group : string = 'default'

count()

Counts the number of records in the specified table that match the given conditions.

public count(string $table[, array<string|int, mixed> $where = [] ]) : int
Parameters
$table : string
$where : array<string|int, mixed> = []
Tags
example

// Example parameters: $table = 'orders'; $where = ['status' => 'completed'];

// You can use this function like this: $count = $this->commonModel->count($table, $where);

// To display the count of matching records: echo "Number of matching records: " . $count;

// If no conditions are specified, the count will include all records in the table: $countAll = $this->commonModel->count($table); echo "Total number of records: " . $countAll;

throws
InvalidArgumentException
since
1.0.0
Return values
int

Returns the count of rows that match the condition

create()

Inserts a new record into the specified table and returns the insert ID.

public create(string $table[, array<string|int, mixed> $data = [] ]) : int

This method allows you to insert a new record into a database table. After the record is inserted, it returns the ID of the inserted row.

Parameters
$table : string
$data : array<string|int, mixed> = []
Tags
example

// Example usage: $table = 'users'; $data = [ 'name' => 'John Doe', 'email' => 'john@example.com', 'status' => 1 ];

// Insert a new user record and get the insert ID: $insertId = $this->commonModel->create($table, $data);

if ($insertId) { echo "New user created with ID: " . $insertId; } else { echo "Failed to create user."; }

throws
InvalidArgumentException
since
1.0.0
Return values
int

Returns the ID of the newly inserted record. If the table doesn't have an auto-incremented primary key, it may return 0.

createMany()

Inserts multiple records into the specified table.

public createMany(string $table, array<string|int, mixed> $data) : mixed

This method inserts an array of data into the table in a batch operation. It is useful for bulk inserts where multiple rows need to be added simultaneously.

Parameters
$table : string

The name of the table where the records will be inserted.

$data : array<string|int, mixed>

A multi-dimensional array where each sub-array represents a row of data to insert. Each sub-array's keys should match the column names. Example: [['name' => 'John', 'email' => 'john@example.com'], ['name' => 'Jane', 'email' => 'jane@example.com']].

Tags
example

// Example usage: $table = 'users'; $data = [ ['name' => 'John Doe', 'email' => 'john@example.com', 'status' => 1], ['name' => 'Jane Doe', 'email' => 'jane@example.com', 'status' => 1] ];

// Insert multiple records into the users table: $result = $this->commonModel->createMany($table, $data);

if ($result) { echo "Records inserted successfully."; } else { echo "Failed to insert records."; }

throws
InvalidArgumentException

If the $data array is empty or invalid.

since
1.1.0
Return values
mixed

Returns true on success, false on failure, or the number of rows inserted (based on the database driver used).

edit()

Updates records in the specified table based on the given conditions.

public edit(string $table[, array<string|int, mixed> $data = [] ][, array<string|int, mixed> $where = [] ]) : bool

This method updates one or more rows in the table where the specified conditions match. The $data array contains the new values for the columns, and the $where array specifies the conditions to find the records to update.

Parameters
$table : string

The name of the table where the records will be updated.

$data : array<string|int, mixed> = []

An associative array of column-value pairs that represent the new values. Example: ['name' => 'John', 'email' => 'john@example.com'].

$where : array<string|int, mixed> = []

An associative array of conditions used to filter the records to be updated. Example: ['id' => 1].

Tags
example

// Example usage: $table = 'users'; $data = ['name' => 'John Doe', 'email' => 'john@example.com']; $where = ['id' => 1];

// Update the user record with ID 1: $result = $this->commonModel->edit($table, $data, $where);

if ($result) { echo "Record updated successfully."; } else { echo "Failed to update record."; }

throws
InvalidArgumentException
since
1.0.0
Return values
bool

Returns true if the update was successful, false otherwise.

isHave()

Checks if there are any records in the specified table that match the given conditions.

public isHave(string $table, array<string|int, mixed> $where) : int
Parameters
$table : string
$where : array<string|int, mixed>
Tags
example

// Example parameters: $table = 'products'; $where = ['category_id' => 10, 'status' => 'active'];

// You can use this function like this: $count = $this->commonModel->isHave($table, $where);

// To display the count of matching records: echo "Number of matching records: " . $count;

throws
InvalidArgumentException
since
1.0.0
Return values
int

Returns the number of rows that match the condition.

lists()

public lists(string $table[, string $select = '*' ][, array<string|int, mixed> $where = [] ][, string $order = 'id ASC' ][, int $limit = 0 ][, int $pkCount = 0 ][, array<string|int, mixed> $like = [] ][, array<string|int, mixed> $orWhere = [] ][, array<string|int, mixed> $joins = [] ][, array<string|int, mixed> $options = ['isReset' => false] ]) : array<string|int, mixed>|null|false
Parameters
$table : string

// table name

$select : string = '*'

// Coluns to select

$where : array<string|int, mixed> = []

// Where conditions

$order : string = 'id ASC'

// Sorting criteria

$limit : int = 0

// Limit on the number of results

$pkCount : int = 0

// Primary key count

$like : array<string|int, mixed> = []

// Like conditions

$orWhere : array<string|int, mixed> = []

// Or conditions

$joins : array<string|int, mixed> = []

// Join operations

$options : array<string|int, mixed> = ['isReset' => false]
Tags
example

// Example parameters: $table= 'users'; $select = 'id, name, email, ...'; $where = ['status' => 1, ...]; $order = 'created_at DESC'; $limit = 10; $pkCount = 1; $like = ['name' => 'John', ...]; $orWhere = ['role' => 'admin', ...]; $joins = [['table' => 'roles', 'cond' => 'users.role_id = roles.id', 'type' => 'left'], ...];

// You can use this function in a Controller or Library like this: $result=$this->commonModel->lists($table, $select, $where, $order, $limit, $pkCount, $like, $orWhere, $joins); // Once the results are returned, you can process the data using a foreach loop: foreach ($result as $row) { echo $row->name . ' - ' . $row->email; }

// If no results are returned, make sure to handle null checks: if (!$result) { echo "No records found."; }

throws
InvalidArgumentException
since
1.0.0
version
1.1.1

Added LIMIT functionality.

version
1.1.4

Added LIKE functionality.

version
1.1.8

Added OR WHERE functionality.

version
1.1.9

Added $options and JOIN functionality.

Return values
array<string|int, mixed>|null|false

Returns the result object on success, null if no results, or false on failure.

notWhereInList()

Retrieves records from the specified table, excluding those where the given key matches any value in the specified array, with optional joins and sorting.

public notWhereInList(string $table[, string $select = '*' ][, array<string|int, mixed> $joins = [] ], string $whereInKey, array<string|int, mixed> $whereInData[, string $orderBy = 'queue ASC' ]) : object
Parameters
$table : string

The name of the table to query.

$select : string = '*'

Columns to select, separated by commas. Defaults to '*' to select all columns.

$joins : array<string|int, mixed> = []

Array of join conditions, where each element is an associative array with keys 'table', 'cond', and 'type' for the join table, condition, and type respectively.

$whereInKey : string

The column to check for exclusion based on the values in $whereInData.

$whereInData : array<string|int, mixed>

An array of values that should be excluded from the results.

$orderBy : string = 'queue ASC'

Column and direction by which to order the results, defaults to 'queue ASC'.

Tags
throws
InvalidArgumentException

If the parameters are invalid.

since
1.1.8
example

// Example parameters: $table = 'orders'; $select = 'id, customer_name, order_date'; $joins = [ ['table' => 'customers', 'cond' => 'orders.customer_id = customers.id', 'type' => 'inner'] ]; $whereInKey = 'order_status'; $whereInData = ['canceled', 'returned']; $orderBy = 'order_date DESC';

// Usage example: $results = $this->commonModel->notWhereInList($table, $select, $joins, $whereInKey, $whereInData, $orderBy);

// Processing the results: foreach ($results as $row) { echo $row->id . ' - ' . $row->customer_name . ' - ' . $row->order_date . '
'; }

// This will retrieve orders that are not 'canceled' or 'returned', sorted by order date in descending order. // If no records match the criteria, an empty result object is returned.

Return values
object

Returns an object containing the result set.

remove()

Deletes records from the specified table based on the given conditions.

public remove(string $table[, array<string|int, mixed> $where = [] ]) : bool

This method deletes one or more rows in the table where the specified conditions match. The $where array is used to specify the conditions for finding the records to be deleted.

Parameters
$table : string

The name of the table from which the records will be deleted.

$where : array<string|int, mixed> = []

An associative array of conditions used to filter the records to be deleted. Example: ['id' => 1].

Tags
example

// Example usage: $table = 'users'; $where = ['id' => 1];

// Delete the user record with ID 1: $result = $this->commonModel->remove($table, $where);

if ($result) { echo "Record deleted successfully."; } else { echo "Failed to delete record."; }

throws
InvalidArgumentException

If the $where array is empty or invalid.

since
1.0.0
Return values
bool

Returns true if the delete operation was successful, false otherwise.

research()

Retrieves records from the specified table that match the given conditions and like patterns.

public research(string $table[, array<string|int, mixed> $like = [] ][, string $select = '*' ][, array<string|int, mixed> $where = [] ]) : object
Parameters
$table : string

The name of the table to query.

$like : array<string|int, mixed> = []

Associative array of LIKE conditions, where the keys are column names and the values are the search patterns.

$select : string = '*'

Columns to select, separated by commas. Defaults to '*' to select all columns.

$where : array<string|int, mixed> = []

Associative array of WHERE conditions.

Tags
throws
InvalidArgumentException

If the parameters are invalid.

since
1.0.0
example

// Example parameters: $table = 'products'; $like = ['name' => 'Gadget']; // Search for 'Gadget' in the 'name' column $select = 'id, name, price'; // Select specific columns $where = ['status' => 'available']; // Filter by 'status'

// Usage example: $results = $this->commonModel->research($table, $like, $select, $where);

// Processing the results: foreach ($results as $row) { echo $row->id . ' - ' . $row->name . ' - $' . $row->price . '
'; }

// If no records match the criteria, an empty result object is returned.

Return values
object

Returns an object containing the result set.

selectOne()

Selects a single record from the database based on conditions.

public selectOne(string $table[, array<string|int, mixed> $where = [] ][, string $select = '*' ][, string $order = 'id ASC' ]) : object|null
Parameters
$table : string
$where : array<string|int, mixed> = []
$select : string = '*'
$order : string = 'id ASC'
Tags
version
1.1.2

Added ORDER functionality.

example

// Example parameters: $table = 'users'; $where = ['id' => 1]; $select = 'id, name, email'; $order = 'created_at DESC';

// You can use this function like this: $result = $this->commonModel->selectOne($table, $where, $select, $order);

// To display the result: if ($result) { echo $result->name . ' - ' . $result->email; } else { echo "No records found."; }

throws
InvalidArgumentException
since
1.0.0
Return values
object|null

Returns the row object on success or null if no result is found.

truncateTable()

Truncates the specified table, removing all records while keeping the table structure intact.

public truncateTable(string $table) : bool
Parameters
$table : string

The name of the table to truncate.

Tags
throws
InvalidArgumentException

If the table name is invalid or not provided.

since
1.1.9
example

// Example usage: $table = 'users';

if ($this->commonModel->truncateTable($table)) { echo 'Table truncated successfully.'; } else { echo 'Failed to truncate the table.'; }

// This will remove all rows from the 'users' table without deleting the table itself.

Return values
bool

Returns true on success, false on failure.

whereInCheckData()

Checks if there are records in the table where a specified column's value matches any of the provided values.

public whereInCheckData(string $att, string $table[, array<string|int, mixed> $where = [] ]) : int
Parameters
$att : string
$table : string
$where : array<string|int, mixed> = []
Tags
example

// Example parameters: $att = 'status'; $table = 'orders'; $where = [1, 2, 3];

// You can use this function like this: $count = $this->commonModel->whereInCheckData($att, $table, $where);

// To display the count of matching records: echo "Number of matching records: " . $count;

throws
InvalidArgumentException
since
1.0.0
Return values
int

Returns the number of rows that match the condition

whereWithJoins()

Retrieves records from the specified table with optional joins, where conditions, like conditions, and ordering, with pagination support.

public whereWithJoins(string $table[, string $select = '*' ][, array<string|int, mixed> $joins = [] ][, array<string|int, mixed> $where = [] ][, string $order = 'id ASC' ][, int $limit = 0 ][, int $pkCount = 0 ][, array<string|int, mixed> $like = [] ][, array<string|int, mixed> $orWhere = [] ]) : object
Parameters
$table : string

The name of the table to query.

$select : string = '*'

The columns to select, separated by commas. Defaults to '*' to select all columns.

$joins : array<string|int, mixed> = []

Array of join conditions, where each element is an associative array with keys 'table', 'cond', and 'type' for the join table, condition, and type respectively.

$where : array<string|int, mixed> = []

An array of conditions to filter the records by.

$order : string = 'id ASC'

The column and direction to order the results by, defaults to 'id ASC'.

$limit : int = 0

The maximum number of records to return, defaults to 0 (no limit).

$pkCount : int = 0

The number of records to skip (for pagination), defaults to 0.

$like : array<string|int, mixed> = []

An array of 'like' conditions for partial matching. Can contain multiple conditions.

$orWhere : array<string|int, mixed> = []

An array of 'or' conditions for additional filtering.

Tags
throws
InvalidArgumentException

If the parameters are invalid.

since
1.1.8
example

// Example parameters: $table = 'orders'; $select = 'orders.id, customers.name, orders.total'; $joins = [ ['table' => 'customers', 'cond' => 'orders.customer_id = customers.id', 'type' => 'inner'] ]; $where = ['orders.status' => 'completed']; $order = 'orders.id DESC'; $limit = 10; $pkCount = 0; $like = ['customers.name' => 'John']; $orWhere = ['orders.total >' => 100];

// Usage example: $results = $this->commonModel->whereWithJoins($table, $select, $joins, $where, $order, $limit, $pkCount, $like, $orWhere);

// Processing the results: foreach ($results as $row) { echo $row->id . ' - ' . $row->name . ' - ' . $row->total . '
'; }

// This will retrieve completed orders where the customer's name contains 'John', or where the order total is greater than 100, joined with customer data, limited to 10 results.

Return values
object

Returns an object containing the result set.


        
On this page

Search results