CommonModel
in package
Table of Contents
Properties
Methods
- __construct() : mixed
- addColumnToTable() : bool
- Adds a new column to the specified table.
- 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.
- drpForeignKey() : bool
- Drop a foreign key to the specified table.
- drpKey() : bool
- Drop a key to the specified table.
- drpPrimaryKey() : bool
- Drop a primary key to the specified table.
- edit() : bool
- Updates records in the specified table based on the given conditions.
- emptyTableDatas() : bool
- Truncates the specified table, removing all records while keeping the table structure intact.
- getTableFields() : array<string|int, mixed>
- Retrieves the fields of the specified table.
- getTableList() : array<string|int, mixed>
- Retrieves the list of tables in the database.
- isHave() : int
- Checks if there are any records in the specified table that match the given conditions.
- lists() : array<string|int, mixed>|null|false
- modifyColumnInfos() : bool
- Modifies the column information of the specified table.
- newDatabase() : bool
- Creates a new database with the specified name.
- newTable() : bool
- Creates a new table in the database with the specified fields and options.
- 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.
- removeColumnFromTable() : bool
- Removes the specified columns from the table.
- removeDatabase() : bool
- Removes the specified database.
- removeTable() : bool
- Removes the specified table from the database.
- 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.
- updateTableName() : bool
- Updates the name of the specified table.
- whereInCheckData() : int
- Checks if there are records in the table where a specified column's value matches any of the provided values.
Properties
$db
public
mixed
$db
$forge
public
mixed
$forge
Methods
__construct()
public
__construct([string $group = 'default' ]) : mixed
Parameters
- $group : string = 'default'
addColumnToTable()
Adds a new column to the specified table.
public
addColumnToTable(string $table, array<string|int, mixed> $fields) : bool
Parameters
- $table : string
-
The name of the table to modify.
- $fields : array<string|int, mixed>
-
An associative array of fields and their data types.
Tags
Return values
bool —Returns true on success, false on failure.
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
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
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
Return values
mixed —Returns true on success, false on failure, or the number of rows inserted (based on the database driver used).
drpForeignKey()
Drop a foreign key to the specified table.
public
drpForeignKey(string $tableName, string $foreignKeyName) : bool
Parameters
- $tableName : string
-
The name of the table to modify.
- $foreignKeyName : string
-
The name of the foreign key to drop.
Tags
Return values
bool —Returns true on success, false on failure.
drpKey()
Drop a key to the specified table.
public
drpKey(string $tableName, string $keyName[, bool $prefixKeyName = true ]) : bool
Parameters
- $tableName : string
-
The name of the table to modify.
- $keyName : string
-
The name of the key to drop.
- $prefixKeyName : bool = true
-
Whether to prefix the key name with the table name.
Tags
Return values
bool —Returns true on success, false on failure.
drpPrimaryKey()
Drop a primary key to the specified table.
public
drpPrimaryKey(string $tableName) : bool
Parameters
- $tableName : string
-
The name of the table to modify.
Tags
Return values
bool —Returns true on success, false on failure.
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
Return values
bool —Returns true if the update was successful, false otherwise.
emptyTableDatas()
Truncates the specified table, removing all records while keeping the table structure intact.
public
emptyTableDatas(string $table) : bool
Parameters
- $table : string
-
The name of the table to truncate.
Tags
Return values
bool —Returns true on success, false on failure.
getTableFields()
Retrieves the fields of the specified table.
public
getTableFields(string $tableName) : array<string|int, mixed>
Parameters
- $tableName : string
-
The name of the table to retrieve fields from.
Tags
Return values
array<string|int, mixed> —Returns an array of field objects.
getTableList()
Retrieves the list of tables in the database.
public
getTableList() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —Returns an array of table names.
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
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
Return values
array<string|int, mixed>|null|false —Returns the result object on success, null if no results, or false on failure.
modifyColumnInfos()
Modifies the column information of the specified table.
public
modifyColumnInfos(string $table, array<string|int, mixed> $fields) : bool
Parameters
- $table : string
-
The name of the table to modify.
- $fields : array<string|int, mixed>
-
An associative array of fields and their new data types.
Tags
Return values
bool —Returns true on success, false on failure.
newDatabase()
Creates a new database with the specified name.
public
newDatabase(string $dbName) : bool
Parameters
- $dbName : string
Tags
Return values
boolnewTable()
Creates a new table in the database with the specified fields and options.
public
newTable(string $table, array<string|int, mixed> $fields[, array<string|int, mixed> $addKeys = ['keys' => [], 'primary' => false, 'unique' => false, 'keyName' => ''] ][, array<string|int, mixed> $foreignKeyFilled = ['field' => '', 'referenceTable' => '', 'referenceField' => '', 'onDelete' => '', 'onUpdate' => '', 'fkName' => ''] ]) : bool
Parameters
- $table : string
-
The name of the table to create.
- $fields : array<string|int, mixed>
-
An associative array of fields and their data types.
- $addKeys : array<string|int, mixed> = ['keys' => [], 'primary' => false, 'unique' => false, 'keyName' => '']
-
Optional keys to add to the table.
- $foreignKeyFilled : array<string|int, mixed> = ['field' => '', 'referenceTable' => '', 'referenceField' => '', 'onDelete' => '', 'onUpdate' => '', 'fkName' => '']
-
Optional foreign key constraints.
Tags
Return values
bool —Returns true on success, 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
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
Return values
bool —Returns true if the delete operation was successful, false otherwise.
removeColumnFromTable()
Removes the specified columns from the table.
public
removeColumnFromTable(string $table, array<string|int, mixed> $fields) : bool
Parameters
- $table : string
-
The name of the table to modify.
- $fields : array<string|int, mixed>
-
An array of column names to remove.
Tags
Return values
bool —Returns true on success, false on failure.
removeDatabase()
Removes the specified database.
public
removeDatabase(string $dbName) : bool
Parameters
- $dbName : string
-
The name of the database to remove.
Tags
Return values
bool —Returns true on success, false on failure.
removeTable()
Removes the specified table from the database.
public
removeTable(string $table) : bool
Parameters
- $table : string
-
The name of the table to remove.
Tags
Return values
bool —Returns true on success, false on failure.
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
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
Return values
object|null —Returns the row object on success or null if no result is found.
updateTableName()
Updates the name of the specified table.
public
updateTableName(string $oldName, string $newName) : bool
Parameters
- $oldName : string
-
The current name of the table.
- $newName : string
-
The new name for the table.
Tags
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
Return values
int —Returns the number of rows that match the condition