\ci4commonmodel CommonModel

Summary

Methods
Properties
Constants
__construct
lists
create
createMany
edit
remove
selectOne
whereInCheckData
isHave
count
research
notWhereInList
getTableList
newTable
removeTable
addColumnToTable
removeColumnFromTable
updateTableName
modifyColumnInfos
emptyTableDatas
getTableFields
newDatabase
removeDatabase
drpPrimaryKey
drpKey
drpForeignKey
db
forge
No public constants found
No protected methods found
No protected properties found
No protected constants found
No private methods found
No private properties found
No private constants found

Properties

$db

$db

$forge

$forge

Methods

__construct()

__construct(string  $group = 'default') : mixed

Parameters

string $group

Returns

mixed —

lists()

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

Parameters

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

Throws

\ci4commonmodel\InvalidArgumentException

Returns

array|null|false —

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()

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.

Parameters

string $table
array $data

Throws

\ci4commonmodel\InvalidArgumentException

Returns

int —

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()

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.

Parameters

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']].

Throws

\ci4commonmodel\InvalidArgumentException

If the $data array is empty or invalid.

Returns

mixed —

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()

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.

Parameters

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].

Throws

\ci4commonmodel\InvalidArgumentException

Returns

bool —

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()

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.

Parameters

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].

Throws

\ci4commonmodel\InvalidArgumentException

If the $where array is empty or invalid.

Returns

bool —

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()

selectOne(string  $table, array  $where = [], string  $select = '*', string  $order = 'id ASC') : object|null

Selects a single record from the database based on conditions.

Parameters

string $table
array $where
string $select
string $order

Throws

\ci4commonmodel\InvalidArgumentException

Returns

object|null —

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

whereInCheckData()

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.

Parameters

string $att
string $table
array $where

Throws

\ci4commonmodel\InvalidArgumentException

Returns

int —

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()

isHave(string  $table, array  $where) : int

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

Parameters

string $table
array $where

Throws

\ci4commonmodel\InvalidArgumentException

Returns

int —

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()

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.

Parameters

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.

Throws

\ci4commonmodel\InvalidArgumentException

Returns

int —

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()

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.

Parameters

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.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

object|null —

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

notWhereInList()

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.

Parameters

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'.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

object|null —

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

getTableList()

getTableList() : array

Retrieves the list of tables in the database.

Returns

array —

Returns an array of table names.

newTable()

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.

Parameters

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.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

removeTable()

removeTable(string  $table) : bool

Removes the specified table from the database.

Parameters

string $table

The name of the table to remove.

Throws

\ci4commonmodel\InvalidArgumentException

If the table name is invalid or not provided.

Returns

bool —

Returns true on success, false on failure.

addColumnToTable()

addColumnToTable(string  $table, array  $fields) : bool

Adds a new column to the specified table.

Parameters

string $table

The name of the table to modify.

array $fields

An associative array of fields and their data types.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

removeColumnFromTable()

removeColumnFromTable(string  $table, array  $fields) : bool

Removes the specified columns from the table.

Parameters

string $table

The name of the table to modify.

array $fields

An array of column names to remove.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

updateTableName()

updateTableName(string  $oldName, string  $newName) : bool

Updates the name of the specified table.

Parameters

string $oldName

The current name of the table.

string $newName

The new name for the table.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

modifyColumnInfos()

modifyColumnInfos(string  $table, array  $fields) : bool

Modifies the column information of the specified table.

Parameters

string $table

The name of the table to modify.

array $fields

An associative array of fields and their new data types.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

emptyTableDatas()

emptyTableDatas(string  $table) : bool

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

Parameters

string $table

The name of the table to truncate.

Throws

\ci4commonmodel\InvalidArgumentException

If the table name is invalid or not provided.

Returns

bool —

Returns true on success, false on failure.

getTableFields()

getTableFields(string  $tableName) : array

Retrieves the fields of the specified table.

Parameters

string $tableName

The name of the table to retrieve fields from.

Throws

\ci4commonmodel\InvalidArgumentException

If the table name is invalid or not provided.

Returns

array —

Returns an array of field objects.

newDatabase()

newDatabase(string  $dbName) : bool

Creates a new database with the specified name.

Parameters

string $dbName

Throws

\ci4commonmodel\InvalidArgumentException

If the database name is invalid or not provided.

Returns

bool —

removeDatabase()

removeDatabase(string  $dbName) : bool

Removes the specified database.

Parameters

string $dbName

The name of the database to remove.

Throws

\ci4commonmodel\InvalidArgumentException

If the database name is invalid or not provided.

Returns

bool —

Returns true on success, false on failure.

drpPrimaryKey()

drpPrimaryKey(string  $tableName) : bool

Drop a primary key to the specified table.

Parameters

string $tableName

The name of the table to modify.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

drpKey()

drpKey(string  $tableName, string  $keyName, bool  $prefixKeyName = true) : bool

Drop a key to the specified table.

Parameters

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.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.

drpForeignKey()

drpForeignKey(string  $tableName, string  $foreignKeyName) : bool

Drop a foreign key to the specified table.

Parameters

string $tableName

The name of the table to modify.

string $foreignKeyName

The name of the foreign key to drop.

Throws

\ci4commonmodel\InvalidArgumentException

If the parameters are invalid.

Returns

bool —

Returns true on success, false on failure.