$db
$db
lists(string $table, string $select = '*', array $where = [], string $order = 'id ASC', int $limit = 0, int $pkCount = 0, array $like = [], array $orWhere = [], array $joins = [], array $options = ['isReset' => false]) : array|null|false
| string | $table | // table name |
| string | $select | // Coluns to select |
| array | $where | // Where conditions |
| string | $order | // Sorting criteria |
| int | $limit | // Limit on the number of results |
| int | $pkCount | // Primary key count |
| array | $like | // Like conditions |
| array | $orWhere | // Or conditions |
| array | $joins | // Join operations |
| array | $options |
Returns the result object on success, null if no results, or false on failure.
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.";
}
Options parameter $options accepts an associative array with the following flags:
isReset (bool): If true, returns a single row object (getRow()). Default is false.distinct (bool): If true, applies DISTINCT to the query. Default is false.isArray (bool): If true, returns the result as an array (getResultArray()). Default is false.count (bool): If true, returns the total number of results (countAllResults()). Default is false.create(string $table, array $data = []) : int
Inserts a new record into the specified table and returns the insert ID.
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.
| string | $table | |
| array | $data |
Returns the ID of the newly inserted record. If the table doesn't have an auto-incremented primary key, it may return 0.
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.";
}
createMany(string $table, array $data) : mixed
Inserts multiple records into the specified table.
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.
| string | $table | The name of the table where the records will be inserted. |
| array | $data | 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']]. |
If the $data array is empty or invalid.
Returns true on success, false on failure, or the number of rows inserted (based on the database driver used).
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.";
}
edit(string $table, array $data = [], array $where = []) : bool
Updates records in the specified table based on the given conditions.
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.
| string | $table | The name of the table where the records will be updated. |
| array | $data | An associative array of column-value pairs that represent the new values. Example: ['name' => 'John', 'email' => 'john@example.com']. |
| array | $where | An associative array of conditions used to filter the records to be updated. Example: ['id' => 1]. |
Returns true if the update was successful, false otherwise.
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.";
}
remove(string $table, array $where = []) : bool
Deletes records from the specified table based on the given conditions.
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.
| string | $table | The name of the table from which the records will be deleted. |
| array | $where | An associative array of conditions used to filter the records to be deleted. Example: ['id' => 1]. |
If the $where array is empty or invalid.
Returns true if the delete operation was successful, false otherwise.
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.";
}
selectOne(string $table, array $where = [], string $select = '*', string $order = 'id ASC') : object|null
Selects a single record from the database based on conditions.
| string | $table | |
| array | $where | |
| string | $select | |
| string | $order |
Returns the row object on success or null if no result is found.
whereInCheckData(string $att, string $table, array $where = []) : int
Checks if there are records in the table where a specified column's value matches any of the provided values.
| string | $att | |
| string | $table | |
| array | $where |
Returns the number of rows that match the condition
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;
isHave(string $table, array $where) : int
Checks if there are any records in the specified table that match the given conditions.
| string | $table | |
| array | $where |
Returns the number of rows that match the condition.
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;
count(string $table, array $where = [], array $like = [], string $select = '', bool $distinct = false) : int
Counts the number of records in the specified table that match the given conditions.
| string | $table | |
| array | $where | |
| array | $like | Optional. Associative array of LIKE conditions. |
| string | $select | Optional. Columns to select. |
| bool | $distinct | Optional. Whether to apply DISTINCT. |
Returns the count of rows that match the condition
Example:
// Example parameters:
$table = 'orders';
$where = ['status' => 'completed'];
$like = ['name' => 'John'];
$select = 'id, name';
$distinct = true;
// You can use this function like this:
$count = $this->commonModel->count($table, $where, $like, $select, $distinct);
// 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;
research(string $table, array $like = [], string $select = '*', array $where = []) : object|null
Retrieves records from the specified table that match the given conditions and like patterns.
| string | $table | The name of the table to query. |
| array | $like | Associative array of LIKE conditions, where the keys are column names and the values are the search patterns. |
| string | $select | Columns to select, separated by commas. Defaults to '*' to select all columns. |
| array | $where | Associative array of WHERE conditions. |
If the parameters are invalid.
Returns the row object on success or null if no result is found.
notWhereInList(string $table, string $select = '*', array $joins = [], string $whereInKey = '', array $whereInData = [], string $orderBy = 'queue ASC') : object|null
Retrieves records from the specified table, excluding those where the given key matches any value in the specified array, with optional joins and sorting.
| string | $table | The name of the table to query. |
| string | $select | Columns to select, separated by commas. Defaults to '*' to select all columns. |
| array | $joins | 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. |
| string | $whereInKey | The column to check for exclusion based on the values in $whereInData. |
| array | $whereInData | An array of values that should be excluded from the results. |
| string | $orderBy | Column and direction by which to order the results, defaults to 'queue ASC'. |
If the parameters are invalid.
Returns the row object on success or null if no result is found.
newTable(string $table, array $fields, array $addKeys = ['keys' => [], 'primary' => false, 'unique' => false, 'keyName' => ''], array $foreignKeyFilled = ['field' => '', 'referenceTable' => '', 'referenceField' => '', 'onDelete' => '', 'onUpdate' => '', 'fkName' => '']) : bool
Creates a new table in the database with the specified fields and options.
| string | $table | The name of the table to create. |
| array | $fields | An associative array of fields and their data types. |
| array | $addKeys | Optional keys to add to the table. |
| array | $foreignKeyFilled | Optional foreign key constraints. |
If the parameters are invalid.
Returns true on success, false on failure.
removeTable(string $table) : bool
Removes the specified table from the database.
| string | $table | The name of the table to remove. |
If the table name is invalid or not provided.
Returns true on success, false on failure.
addColumnToTable(string $table, array $fields) : bool
Adds a new column to the specified table.
| string | $table | The name of the table to modify. |
| array | $fields | An associative array of fields and their data types. |
If the parameters are invalid.
Returns true on success, false on failure.
removeColumnFromTable(string $table, array $fields) : bool
Removes the specified columns from the table.
| string | $table | The name of the table to modify. |
| array | $fields | An array of column names to remove. |
If the parameters are invalid.
Returns true on success, false on failure.
updateTableName(string $oldName, string $newName) : bool
Updates the name of the specified table.
| string | $oldName | The current name of the table. |
| string | $newName | The new name for the table. |
If the parameters are invalid.
Returns true on success, false on failure.
modifyColumnInfos(string $table, array $fields) : bool
Modifies the column information of the specified table.
| string | $table | The name of the table to modify. |
| array | $fields | An associative array of fields and their new data types. |
If the parameters are invalid.
Returns true on success, false on failure.
emptyTableDatas(string $table) : bool
Truncates the specified table, removing all records while keeping the table structure intact.
| string | $table | The name of the table to truncate. |
If the table name is invalid or not provided.
Returns true on success, false on failure.
getTableFields(string $tableName) : array
Retrieves the fields of the specified table.
| string | $tableName | The name of the table to retrieve fields from. |
If the table name is invalid or not provided.
Returns an array of field objects.
removeDatabase(string $dbName) : bool
Removes the specified database.
| string | $dbName | The name of the database to remove. |
If the database name is invalid or not provided.
Returns true on success, false on failure.
drpKey(string $tableName, string $keyName, bool $prefixKeyName = true) : bool
Drop a key to the specified table.
| string | $tableName | The name of the table to modify. |
| string | $keyName | The name of the key to drop. |
| bool | $prefixKeyName | Whether to prefix the key name with the table name. |
If the parameters are invalid.
Returns true on success, false on failure.
drpForeignKey(string $tableName, string $foreignKeyName) : bool
Drop a foreign key to the specified table.
| string | $tableName | The name of the table to modify. |
| string | $foreignKeyName | The name of the foreign key to drop. |
If the parameters are invalid.
Returns true on success, false on failure.