BaseModel

__construct()

<?php
public function __construct() {
    $configdb = config('db');  // Use config from config.php with key 'db'
    $this->db = $this->loadDatabaseDriver($configdb['db_driver'], $configdb);
    unset($configdb);
}
  • Initializes the database connection using the configuration from the config file.

  • Loads the appropriate database driver.

_schema()

<?php
public function _schema() {
    return [];
}

Defines the structure of the model by returning a schema array. By default, it returns an empty array, but child classes can override this method to specify the model's data structure.

_table()

Returns the name of the database table associated with the model. It helps establish the connection between the model and its corresponding table.

_columns()

Returns the list of columns that are allowed to be filled with data. These columns are defined in the child model and determine which fields can be updated or inserted to prevent unauthorized modifications.

fill()

  • Filters the input data to only include columns that are allowed to be filled.

  • $data: Data to filter.

row()

Fetches a single row from the database based on the specified conditions.

$table: Name of the table.

$where: Query conditions.

$params: Optional parameters.

return: Data row or false if no result.

rowField()

Fetches a single row with specific fields from the database based on the specified conditions.

$table: Name of the table.

$fields: Fields to query (optional).

$where: Query conditions.

$params: Optional parameters.

return: Data row or false if no result.

list()

Fetches multiple rows from the database based on the specified conditions, order, and pagination.

$table: Name of the table.

$where: Query conditions.

$params: Optional parameters.

$orderBy: ORDER BY clause (optional).

$page: Page number for pagination.

$limit: Number of results per page (optional).

return: List of data rows.

listpaging()

Fetches multiple rows with pagination from the database based on the specified conditions, order, and pagination.

$table: Name of the table.

$where: Query conditions.

$params: Optional parameters.

$orderBy: ORDER BY clause (optional).

$page: Page number for pagination.

$limit: Number of results per page (optional).

return: List of data rows.

listfieldpaging()

Fetches multiple rows with specific fields and pagination from the database based on the specified conditions, order, and pagination.

$table: Name of the table.

$fields: Fields to fetch.

$where: Query conditions.

$params: Optional parameters.

$orderBy: ORDER BY clause (optional).

$page: Page number for pagination.

$limit: Number of results per page (optional).

return: List of data rows.

add()

  • Inserts a new row into the database.

$table: Name of the table.

$data: Data to insert.

return: Success or failure.

set()

Updates existing rows in the database based on the specified conditions.

$table: Name of the table.

$data: Data to update.

$where: Update conditions.

return: Number of affected rows.

del()

Deletes rows from the database based on the specified conditions.

$table: Name of the table.

$where: Delete conditions.

return: Number of affected rows

query()

Executes a custom SQL query.

$query: The SQL query string.

$params: An array of parameters to bind to the query (optional).

return: Result of the query (used for SELECT, INSERT, UPDATE, DELETE)

lastInsertId()

Returns the ID of the last inserted row.

count()

Counts the number of rows in the database based on the specified conditions.

$table: The name of the table.

$where: The WHERE clause to filter records (optional).

$params: An array of values corresponding to the parameters in the WHERE clause (optional).

return: Number of records in the table.

Example

UsersModel.php

Create UsersModel and add the following code:

UsersController.php

In UsersController.php, you can utilize UsersModel to interact with the database and seamlessly pass the retrieved data to the view for rendering.

Last updated

Was this helpful?