Documentation

CBaseController extends CDB
in package

This class handles the input requests that are delegated to the respective classes and provides the output.

Implementation the basic Controller for all Modules.

Tags

Table of Contents

ALERT  = 1
CRITICAL  = 2
DB_ASSOC  = MYSQLI_ASSOC
DB_BOTH  = MYSQLI_BOTH
DB_NUM  = MYSQLI_NUM
DEBUG  = 7
EMERGENCY  = 0
ERROR  = 3
INFO  = 6
NOTICE  = 5
WARNING  = 4
$all_db_modules  : array<string|int, mixed>|null
$all_db_tables  : array<string|int, mixed>|null
$autocommit  : bool
$bind_params_types  : array<string|int, mixed>
$bind_params_values  : array<string|int, mixed>
$cache  : CCache|null
$database_name  : string
$dbkey  : string|null
$dbname  : array<string|int, mixed>
$dbpassword  : string
$dbusername  : string
$handle  : array<string|int, mixed>
$hostname  : string
$items_view  : string
$last_id  : int
$method  : string
$model  : mixed
$module_name  : string
$port  : int
$related_field_names  : array<string|int, mixed>
$related_tables_required  : array<string|int, mixed>
$request  : CRequest
$response  : CResponse
$results  : array<string|int, mixed>
$stmt  : mixed
$table  : string
$table_filter  : string
$user  : CUser|null
__construct()  : void
Constructor
__destruct()  : mixed
Destroys any allocated resources
addItemRequired()  : void
For POST, PUT, PATCH methods add some limitations as min or max value or a specific rule with regular expression
addRelatedFieldName()  : void
For the GET method, add the index of the foreign key with the field name ($related_item_name) indicated in the related table ($table)
alert()  : void
Action must be taken immediately.
beginTransaction()  : void
Start DB transaction It is recommended to use transitions in case of multiple SQL statements, which, if at least one fails, the entire set of statements can be canceled
commit()  : void
Validate SQL operations performed after calling beginTransaction()
connect()  : void
Set a parameter for a DB connection
critical()  : void
Critical conditions.
debug()  : void
Detailed debug information.
decrypt()  : string
Decrypt a value from DB
deleteRecord()  : bool
Delete records from a DB table
disconnect()  : void
Releases the resource to connect to the DB
disconnectAll()  : void
Releases all the resources of the previously opened DBs
emergency()  : void
System is unusable.
encrypt()  : string
Reversibly encrypts a value to DB
error()  : void
Runtime errors that do not require immediate action but should typically be logged and monitored.
exec()  : string
Call the appropriate method (GET,POST,PUT,PATCH,DELETE,...) e return the results
execDELETE()  : array<string|int, mixed>
Manager of HTTP Method DELETE
execGET()  : array<string|int, mixed>|null
Manager of HTTP Method GET (Read data)
execHEAD()  : array<string|int, mixed>
Manager of HTTP Method HEAD (as GET but without body)
execOPTIONS()  : array<string|int, mixed>
Manager of HTTP Method OPTIONS
execPATCH()  : array<string|int, mixed>
Manager of HTTP Method PATCH (update without full data required) Update of one or more records of the table
execPUT()  : array<string|int, mixed>
Manager of HTTP Method PUT (update with full data required) Update of one or more records of the table
execute()  : void
Execution of SQL with binding
ExistsTable()  : bool
Check if the table passed as a parameter exists (including the prefix)
getAffectedRows()  : int
Returns how many rows were affected by the last operation performed
getAllModules()  : void
Import from DB all record of module table
getAllTables()  : void
Returns an array containing the table name as an index and an array containing the name and type of the columns as a value
getArrayByName()  : array<string|int, mixed>|null
After executing a query it returns an array whose row index has the column value "$ name" and the rest of the fields are the cell value:
getArrayItemOfElements()  : array<string|int, mixed>
Given an array containing a series of records, it returns an array containing only the values ​​of a specific field
getCacheArrayByName()  : array<string, mixed>|null
After executing a query it returns an array whose row index has the column value "$ name" and the rest of the fields are the cell value:
getCachedDataTable()  : void
getCachedIDsArray()  : array<string|int, string>|null
getColumnNames()  : array<string|int, string>|null
Returns an array containing the names of all columns for the last query executed
getControllerName()  : string
Include the Controller class more appropriated and return the fullname
getCurrentDate()  : string
Return the current date for this DB
getCurrentDateTime()  : string
Return the current date time for this DB
getCurrentDBKey()  : string|null
Returns the key of the current DB
getCurrentModel()  : mixed
Get the current Model Object (CBaseModel or sons)
getDataTable()  : int|null
Given a previously executed query, it returns the result in a two-dimensional array
getDataValue()  : string|null
Returns a query value relative to a row and a column
getDBHandle()  : mixed
Returns the current handle of the DB
getDBResultAssoc()  : int
Returns the constant DB_ASSOC which establishes that the query must return an associative array
getDBResultBoth()  : int
Returns the constant DB_ASSOC which establishes that the query must return both an associative array and a numeric array
getDBResultNum()  : int
Returns the constant DB_ASSOC which establishes that the query must return a numeric array
getDBTable()  : string
Returns the name of the current main table
getFetchArray()  : array<string|int, mixed>|null
Reads a row of the position database $pos_row of the table of the previously executed query and returns it as an array with both numeric and associative numbers
getFetchAssoc()  : array<string|int, mixed>|null
Gets a row of the position database $pos_row of the table of the previously executed query and returns it as an associative array in an associative array whose index is given by the name of the column
getFetchRow()  : array<string|int, mixed>|null
Gets a row of the $pos_row position db of the previously executed query table and returns it as an array with a numeric index
getIdsArray()  : array<string|int, mixed>
Creates an array containing all elements found based on a DB table name or index
getInfoAllTables()  : array<string|int, mixed>
Returns an array containing the list of all the tables of the current DB
getLastDBErrorNumber()  : int
Reads the error number issued by the DB
getLastDBErrorString()  : string
Gets any error issued by the DB
getLastID()  : int|null
Returns the ID of the last inserted item
getModelName()  : string
Include the Model class more appropriated and return the fullname
getModuleElement()  : string
Return the fullname of the class more appropriated
getNextID()  : int|null
Returns the from value of the next id to use in the insert NOTE: in Mysql you can set the primary key as autoincrement, than is not needed to set the table and it returns always null
getQueryDataTable()  : int|null
Given a previously executed query, it returns the result in a two-dimensional array
getQueryDataValue()  : string|null
Queries the DB and returns a specific value
getQueryFetchAssoc()  : array<string|int, mixed>|null
Queries the DB and returns an associative array containing the result
getQueryFetchRow()  : array<string|int, mixed>|null
Queries the DB and returns a row from the DB as an array having a numeric index
getTableColumnNames()  : array<string|int, string>
Returns an array containing the names of all columns of $tablename
getTotalColumns()  : int|bool
Returns the number of columns of a previously executed query
getTotalRows()  : int|bool
Reads the total of rows of a previously executed query
info()  : void
Interesting events.
insertRecord()  : bool
Make an entry in the DB
log()  : void
Logs with an arbitrary level.
notice()  : void
Normal but significant events.
prepare()  : void
Prepares an SQL statement for execution
query()  : bool
Query the DB with SQL
rollback()  : void
Cancel all SQL operations performed after calling beginTransaction ()
setAutoCommit()  : void
Set autocommit, as opposed to transitions, if true each operation is self-consistent and validated
setCurrentDBKey()  : bool
Set the current DB key NOTE: generally it is not necessary to use a DB key but it can be useful when there are multiple databases to manage
setDBTable()  : void
Sets the name of the reference table
setExcludedControllers()  : void
Delete some controllers derived from DB tables
setItemsRequired()  : void
For POST, PUT, PATCH methods add some limitations as min or max value or a specific rule with regular expression
setRelatedFieldNames()  : void
For the GET method, it replaces the index of the foreign key with the value of related field ($related_item_name) indicated in the related table ($table)
setRelatedTables()  : void
For the GET method, sets the name of the related tables to be presented for each single element of the current table
setUser()  : void
Set a user manager object (CUser or childs)
updateRecord()  : bool
Make an update in the DB
warning()  : void
Exceptional occurrences that are not errors.
_deleteDB()  : bool
Delete a record into a DB table
_getCachedData()  : mixed
Get a content in cache if exists
_getCacheKey()  : string
Get a content in cache if exists
_getContentFromDB()  : array<string|int, mixed>
Get a content in cache if exists
_insertDB()  : bool
Insert a new record into a DB table
_invalidateRelatedCache()  : void
Invalid all cache related to current table and all related
_realConnection()  : mixed
Check the connection to the DB, if not there, try to connect
_revalidateHttpCache()  : void
Invalidating the http cache N.B.: it is different from the internal data cache
_setCachedData()  : void
Get a content in cache if exists
_updateDB()  : bool
Modify a record into a DB table
changeCurrentConnectionKey()  : void
Change the current db key (if you have more than on DB)
changeResult()  : mixed
Change data and/or execute code before send data You can use: $this->method for distinguish the method used CRequest::setHtmlFrame(string $htmlframe) for change the html output file CRequest::getAttributes() for know other specific values from url
convertDBType2BindType()  : string|null
Converts from the type of the DB (INT, VARCHAR, FLOAT, BLOB) to the type of Bind (i, s, d, b)
doAfterOperation()  : void
After an Insert, Modify or Delete, derived classes can use this method to perform additional operations
execPOST()  : array<string|int, mixed>
Manager of HTTP Method POST (insert)
existsTableColumns()  : bool
Check if a named list of fields exists in a db table
getConditions()  : string
Import all necessary conditions based on filters and any additional conditions
getConditionsByFilters()  : string
Set the conditions according to the input filters
getConnectionError()  : int
Set the current db key (if you have more than on DB)
getCurrentConnectionKey()  : string|null
Get the current db key
isConnected()  : bool
Check the connection is active without error
setDBColumns()  : void
Returns an array containing the list of columns of a table each columns contains: - 'field' the name of column, - 'type' the type of column, - 'null' if accept the NULL DB value,
setItemsView()  : void
setTable()  : void
Set table name and keep all table fields with types NOTE: if $table have "-rel" the foreign key with id the keys will be replaced with their names

Constants

Properties

$database_name

protected static string $database_name
Tags

$dbkey

protected static string|null $dbkey = null
Tags

$dbname

protected static array<string|int, mixed> $dbname = []
Tags

$dbpassword

protected static string $dbpassword
Tags

$dbusername

protected static string $dbusername
Tags

$handle

protected static array<string|int, mixed> $handle = []
Tags
protected array<string|int, mixed> $related_tables_required = ['*']
Tags

$results

protected static array<string|int, mixed> $results = []
Tags

Methods

__construct()

Constructor

public __construct(CRequest $request, CResponse $response) : void
Parameters
$request : CRequest

object that contains the current request, that is, all the input data

$response : CResponse

object that will contain the response processed by the current object

Tags
throws
Exception

if the method (GET,POST) or the table/module is not authorized for the user

Return values
void

__destruct()

Destroys any allocated resources

public __destruct() : mixed
Tags
Return values
mixed

addItemRequired()

For POST, PUT, PATCH methods add some limitations as min or max value or a specific rule with regular expression

public addItemRequired(string $name, array<string|int, mixed> $options[, string $table = null ]) : void
Parameters
$name : string

name of item to add

$options : array<string|int, mixed>

array contains min,max,rule values

$table : string = null

table of DB or Controller (if the value is null or not declared, the table name is the table request)

Tags
see
setItemsRequired()
example

$controller = CBaseController::getControllerName(); $rest = new $controller( ); $rest->addItemRequired( 'freq', ['rule'=>'/[daily|weekly|monthly]/'],'page' ); $rest->addItemRequired( 'age', ['min'=>16, 'max'=35], 'student' );

Return values
void

addRelatedFieldName()

For the GET method, add the index of the foreign key with the field name ($related_item_name) indicated in the related table ($table)

public addRelatedFieldName(string $related_item_name[, string $table = null ]) : void
Parameters
$related_item_name : string

The name of the related table

$table : string = null

The name of the field in the related table (if the value is null or not declared, the table name is the table request)

Tags
see
setRelatedFieldNames()

to indicate all fields of all tables

example

$controller_name = CBaseController::getControllerName(); $cntrl = new $controller_name( ); $cntrl->addRelatedFieldName( 'name', 'preference' ); // id_preference => [table:field] => preference:name

Return values
void

alert()

Action must be taken immediately.

public static alert(string $message[, array<string|int, mixed> $context = [] ]) : void

Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up.

Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

beginTransaction()

Start DB transaction It is recommended to use transitions in case of multiple SQL statements, which, if at least one fails, the entire set of statements can be canceled

public abstract beginTransaction() : void

NOTE: before use benginTransaction() you must be sure that the AutoCommit is false with setAutoCommit(false)

Tags
see
setAutoCommit()
see
commit()
see
rollback()
Return values
void

commit()

Validate SQL operations performed after calling beginTransaction()

public abstract commit() : void
Tags
see
setAutoCommit()
see
beginTransaction()
see
rollback()
Return values
void

connect()

Set a parameter for a DB connection

public static connect(string $dbname, string $username, string $password[, string $hostname = 'localhost' ][, int|string $port = DB_PORT ]) : void
Parameters
$dbname : string

name of the database inside the DBMS

$username : string

name of the user connecting to the DB

$password : string

DB password

$hostname : string = 'localhost'

name or IP address of the server (if it is on the same computer it is usually in "localhost")

$port : int|string = DB_PORT

communication port: if left empty, it takes the default value (3306)

Tags
Return values
void

critical()

Critical conditions.

public static critical(string $message[, array<string|int, mixed> $context = [] ]) : void

Example: Application component unavailable, unexpected exception.

Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

debug()

Detailed debug information.

public static debug(string $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

decrypt()

Decrypt a value from DB

public abstract decrypt(string $val[, string $name = '' ]) : string

NOTE: if $val==$name decrypts the DB field name

Parameters
$val : string

value or name of the field to decrypt

$name : string = ''

name of the field that is returned

Tags
Return values
string

DB instruction to decrypt $ val

deleteRecord()

Delete records from a DB table

public abstract deleteRecord(string $table, string $conditions, array<string|int, string> $conditions_values, array<string|int, mixed> $types) : bool
Parameters
$table : string

name of the database table on which to delete one or more records

$conditions : string

conditions of the where with any "?" in the parameters

$conditions_values : array<string|int, string>

values ​​present in the WHERE condition that will have to replace the "?"

$types : array<string|int, mixed>
Tags
Return values
bool

result of the operation, true if successful

disconnect()

Releases the resource to connect to the DB

public abstract static disconnect([string|null $dbkey = null ]) : void
Parameters
$dbkey : string|null = null

key of db to disconnect (optional), if not send or is null keep the current db. Normally you have only one DB, in this case you can ignore $dbkey parameter

Tags
Return values
void

disconnectAll()

Releases all the resources of the previously opened DBs

public abstract static disconnectAll() : void
Tags
Return values
void

emergency()

System is unusable.

public static emergency(string $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

encrypt()

Reversibly encrypts a value to DB

public abstract encrypt(string $val[, string $name = '' ]) : string

NOTE: if $val==$name encrypts the name of the DB field

Parameters
$val : string

value or name of the field to be encrypted

$name : string = ''

name of the field that is returned

Tags
Return values
string

DB instruction to encrypt $val

error()

Runtime errors that do not require immediate action but should typically be logged and monitored.

public static error(string $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

exec()

Call the appropriate method (GET,POST,PUT,PATCH,DELETE,...) e return the results

public exec() : string
Tags
throws
Exception

for Access denied or a invalid method used

Return values
string

all data results

execDELETE()

Manager of HTTP Method DELETE

public execDELETE() : array<string|int, mixed>
Tags
throws
Exception

if no records were found

Return values
array<string|int, mixed>

message result of delete operation

execGET()

Manager of HTTP Method GET (Read data)

public execGET() : array<string|int, mixed>|null
Tags
throws
Exception

if is not set a valid module or if the element not exist

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

results of query

execHEAD()

Manager of HTTP Method HEAD (as GET but without body)

public execHEAD() : array<string|int, mixed>
Tags
throws
Exception

if is not set a valid module or if the element not exist

Return values
array<string|int, mixed>

result of the operation

execOPTIONS()

Manager of HTTP Method OPTIONS

public execOPTIONS() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

array with a structure of table associated to module or if module=all, the list of all modules available

execPATCH()

Manager of HTTP Method PATCH (update without full data required) Update of one or more records of the table

public execPATCH() : array<string|int, mixed>
Tags
throws
Exception

if the element not exist

Return values
array<string|int, mixed>

message result of update operation

execPUT()

Manager of HTTP Method PUT (update with full data required) Update of one or more records of the table

public execPUT() : array<string|int, mixed>
Tags
throws
Exception

if the element not exist

Return values
array<string|int, mixed>

message result of update operation

execute()

Execution of SQL with binding

public abstract execute([array<string|int, mixed>|null $bind_param_values = null ][, array<string|int, string>|null $bind_param_types = null ]) : void
Parameters
$bind_param_values : array<string|int, mixed>|null = null
$bind_param_types : array<string|int, string>|null = null
Tags
Return values
void

ExistsTable()

Check if the table passed as a parameter exists (including the prefix)

public abstract ExistsTable(string $table) : bool
Parameters
$table : string

name of the table

Tags
Return values
bool

true if it exists, false if it does not exist

getAffectedRows()

Returns how many rows were affected by the last operation performed

public abstract getAffectedRows() : int
Tags
Return values
int

total of rows affected by the last operation

getAllModules()

Import from DB all record of module table

public getAllModules(array<string|int, mixed> &$all_db_modules) : void
Parameters
$all_db_modules : array<string|int, mixed>
Tags
Return values
void

getAllTables()

Returns an array containing the table name as an index and an array containing the name and type of the columns as a value

public abstract getAllTables(array<string|int, string> &$db_tables) : void
Parameters
$db_tables : array<string|int, string>

list of tables to be returned

Tags
Return values
void

getArrayByName()

After executing a query it returns an array whose row index has the column value "$ name" and the rest of the fields are the cell value:

public getArrayByName([string|int $name = 'name' ][, string|null $fieldname_value = null ]) : array<string|int, mixed>|null
Parameters
$name : string|int = 'name'

name or position of the select column that must have as index (default is' 'name')

$fieldname_value : string|null = null

name of the field to fetch the value from, if null returns all data in an array

Tags
example

if the sql returns: name val ========== Max 10 Min 1 The getArrayByName('name','val') function will return array('Max'=>10,'Min'=1)

example

if the sql returns: id name val ============ 1 Max 10 2 Min 1 The getArrayByName('name') function will return array('Max'=>array(ITEM_ID=>1,'val'=>10),'Min'=array(ITEM_ID=>2,'val'=>1))

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

array containing the list name => value in which value can be a scalar or an associative array if it must contain more values or it returns null if there are no elements or an empty array if the field name does not exist among the results of the last query executed

getArrayItemOfElements()

Given an array containing a series of records, it returns an array containing only the values ​​of a specific field

public getArrayItemOfElements(array<string|int, mixed> &$elements, string $fieldname) : array<string|int, mixed>
Parameters
$elements : array<string|int, mixed>

all records to be analyzed

$fieldname : string

name of the field / column containing the data to be extrapolated

Tags
Return values
array<string|int, mixed>

array containing the list of values ​​found or null if it found nothing

getCacheArrayByName()

After executing a query it returns an array whose row index has the column value "$ name" and the rest of the fields are the cell value:

public getCacheArrayByName(string $sql[, array<string|int, mixed>|null $bind_param_values = null ][, array<string|int, string>|null $bind_param_types = null ][, string $area_name = '' ][, bool $is_global = false ][, string $name = 'name' ][, string|null $fieldname_value = null ]) : array<string, mixed>|null
Parameters
$sql : string

string containing the query to execute

$bind_param_values : array<string|int, mixed>|null = null

deferred values ​​of the query

$bind_param_types : array<string|int, string>|null = null

DB data types

$area_name : string = ''

string containing area/module name in the cache

$is_global : bool = false

true for a global cache, false for single user cache

$name : string = 'name'

name of the select column that must have as index (default is' 'name')

$fieldname_value : string|null = null

name of the field to fetch the value from, if null returns all data in an array

Tags
example

if the sql returns: name val ========== Max 10 Min 1 The getArrayByName('name','val') function will return array('Max'=>10,'Min'=1)

example

if the sql returns: id name val ============ 1 Max 10 2 Min 1 The getArrayByName('name') function will return array('Max'=>array(ITEM_ID=>1,'val'=>10),'Min'=array(ITEM_ID=>2,'val'=>1))

Return values
array<string, mixed>|null

array containing the list name => value in which value can be a scalar or an associative array if it must contain more values or it returns null if there are no elements or an empty array if the field name does not exist among the results of the last query executed

getCachedDataTable()

public getCachedDataTable(mixed &$data, string $sql[, array<string|int, mixed>|null $bind_param_values = null ][, array<string|int, string>|null $bind_param_types = null ][, int $type = null ][, string|null $with_index = null ][, string $area_name = '' ][, bool $is_global = false ]) : void
Parameters
$data : mixed

array containing the records found

$sql : string

string containing the query to execute

$bind_param_values : array<string|int, mixed>|null = null

deferred values ​​of the query

$bind_param_types : array<string|int, string>|null = null

DB data types

$type : int = null

it can assume the values: DB_ASSOC, DB_NUM and DB_BOTH equivalent to the type of array it must return, respectively:

  • numeric index (returned from getDBResultNum())
  • associative index (returned from getDBResultAssoc())
  • both indices (returned from getDBResultBoth()) by default is value returned from getDBResultAssoc(), that is, it returns an associative array
$with_index : string|null = null

indexes of the output array (for example it could be "id"), if specified the numeric value of the row is replaced with the value indicated in the index

$area_name : string = ''

string containing area/module name in the cache

$is_global : bool = false

true for a global cache, false for single user cache

Tags
Return values
void

getCachedIDsArray()

public getCachedIDsArray(string $sql[, array<string|int, mixed>|null $bind_param_values = null ][, array<string|int, string>|null $bind_param_types = null ][, string $area_name = '' ][, bool $is_global = false ]) : array<string|int, string>|null
Parameters
$sql : string

string containing the query to execute

$bind_param_values : array<string|int, mixed>|null = null

deferred values ​​of the query

$bind_param_types : array<string|int, string>|null = null

DB data types

$area_name : string = ''

string containing area/module name in the cache

$is_global : bool = false

true for a global cache, false for single user cache

Tags
Return values
array<string|int, string>|null

getColumnNames()

Returns an array containing the names of all columns for the last query executed

public abstract getColumnNames() : array<string|int, string>|null
Tags
Return values
array<string|int, string>|null

the associative having as index and value the column names of the queries or null if it fails or null if the table was not found

getControllerName()

Include the Controller class more appropriated and return the fullname

public static getControllerName() : string
Tags
Return values
string

the fullname of class Controller required

getCurrentDate()

Return the current date for this DB

public static getCurrentDate() : string
Tags
Return values
string

the current date for this DB

getCurrentDateTime()

Return the current date time for this DB

public static getCurrentDateTime() : string
Tags
Return values
string

the current date time for this DB

getCurrentDBKey()

Returns the key of the current DB

public getCurrentDBKey() : string|null
Tags
Return values
string|null

the key of the current DB

getCurrentModel()

Get the current Model Object (CBaseModel or sons)

public getCurrentModel() : mixed
Tags
Return values
mixed

the current model (is a CBaseModel object or an his derived)

getDataTable()

Given a previously executed query, it returns the result in a two-dimensional array

public abstract getDataTable(array<string|int, mixed> &$data[, int|null $type = null ][, string|null $with_index = null ]) : int|null
Parameters
$data : array<string|int, mixed>

array containing the records found

$type : int|null = null

it can assume the values: DB_ASSOC, DB_NUM and DB_BOTH equivalent to the type of array it must return, respectively:

  • numeric index (returned from getDBResultNum())
  • associative index (returned from getDBResultAssoc())
  • both indices (returned from getDBResultBoth()) by default is value returned from getDBResultAssoc(), that is, it returns an associative array
$with_index : string|null = null

indexes of the output array (for example it could be "id"), if specified the numeric value of the row is replaced with the value indicated in the index

Tags
Return values
int|null

the total of records found or null if the query is not valid or return empty results

getDataValue()

Returns a query value relative to a row and a column

public abstract getDataValue([int|string $name_col = '0' ][, int|null $pos_row = null ]) : string|null
Parameters
$name_col : int|string = '0'

column name or numeric index (default 0)

$pos_row : int|null = null

position of the row starting from 0, if it is null or not indicated it reads the current value

Tags
Return values
string|null

value of the indicated row and column relative to the previously executed query, null if it does not exist

getDBHandle()

Returns the current handle of the DB

public static getDBHandle() : mixed
Tags
Return values
mixed

current DB handle or null if not available

getDBResultAssoc()

Returns the constant DB_ASSOC which establishes that the query must return an associative array

public abstract getDBResultAssoc() : int
Tags
Return values
int

getDBResultBoth()

Returns the constant DB_ASSOC which establishes that the query must return both an associative array and a numeric array

public abstract getDBResultBoth() : int
Tags
Return values
int

getDBResultNum()

Returns the constant DB_ASSOC which establishes that the query must return a numeric array

public abstract getDBResultNum() : int
Tags
Return values
int

getDBTable()

Returns the name of the current main table

public getDBTable() : string
Tags
Return values
string

name of the main reference table

getFetchArray()

Reads a row of the position database $pos_row of the table of the previously executed query and returns it as an array with both numeric and associative numbers

public abstract getFetchArray([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$pos_row : int|null = null

position of the line to read, if omitted is the current element

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

the array of the row indicated by $ pos_row (having both a numeric index and containing the column name) or null where the result is not valid (e.g. if the index is outside the range of the table)

getFetchAssoc()

Gets a row of the position database $pos_row of the table of the previously executed query and returns it as an associative array in an associative array whose index is given by the name of the column

public abstract getFetchAssoc([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$pos_row : int|null = null

position of the line to read, the current element if it is omitted

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

associative array of the row indicated by $pos_row or null in which the result is not valid e.g.: if the index is outside the range of the table

getFetchRow()

Gets a row of the $pos_row position db of the previously executed query table and returns it as an array with a numeric index

public abstract getFetchRow([int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$pos_row : int|null = null

posizione della riga da leggere, se omesso e' l'elemento corrente

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

the array of the indicated row (having a numeric index) from $pos_row or null in which the result is not valid (e.g. if the index is outside the range of the table)

getIdsArray()

Creates an array containing all elements found based on a DB table name or index

public getIdsArray([int|string $item = 0 ][, int|string|null $idx = null ]) : array<string|int, mixed>
Parameters
$item : int|string = 0

index (int or string) of array to be returned

$idx : int|string|null = null

name or index of the field (by default it is 0, which is the first element)

Tags
Return values
array<string|int, mixed>

containing all values ​​indicated by $idx

getInfoAllTables()

Returns an array containing the list of all the tables of the current DB

public abstract getInfoAllTables([string|null $table = null ][, bool $with_sizes = false ]) : array<string|int, mixed>
Parameters
$table : string|null = null

base table name to get all those that use it

$with_sizes : bool = false

if "false" it does not calculate the total of the records (by default it is true)

Tags
Return values
array<string|int, mixed>

information relating to the tables in the DB

getLastDBErrorNumber()

Reads the error number issued by the DB

public abstract static getLastDBErrorNumber() : int
Tags
Return values
int

integer value containing the error issued by the DB

getLastDBErrorString()

Gets any error issued by the DB

public abstract static getLastDBErrorString() : string
Tags
Return values
string

containing the error issued by the DB

getLastID()

Returns the ID of the last inserted item

public getLastID() : int|null
Tags
Return values
int|null

if the operation was successful, it returns the id of the last element inserted

getModelName()

Include the Model class more appropriated and return the fullname

public static getModelName() : string
Tags
Return values
string

the fullname of class Model required

getModuleElement()

Return the fullname of the class more appropriated

public static getModuleElement(string $class_type) : string
Parameters
$class_type : string

class type (normally 'Controller' or 'Model')

Tags
Return values
string

the fullname of class required

getNextID()

Returns the from value of the next id to use in the insert NOTE: in Mysql you can set the primary key as autoincrement, than is not needed to set the table and it returns always null

public abstract getNextID([string $table = '' ]) : int|null
Parameters
$table : string = ''

the name of counter associated to SEQUENCE

Tags
Return values
int|null

the next ID of the table

getQueryDataTable()

Given a previously executed query, it returns the result in a two-dimensional array

public getQueryDataTable(array<string|int, mixed> &$data, string $sql[, array<string|int, mixed> $bind_param_values = null ][, array<string|int, string> $bind_param_types = null ][, int|null $type = null ][, string|null $with_index = null ]) : int|null
Parameters
$data : array<string|int, mixed>

array containing the records found

$sql : string

string containing the query to execute

$bind_param_values : array<string|int, mixed> = null

deferred values ​​of the query

$bind_param_types : array<string|int, string> = null

DB data types

$type : int|null = null

it can assume the values: CDB::DB_ASSOC, CDB::DB_NUM and CDB::DB_BOTH equivalent to the type of array it must return, respectively:

  • numeric index (returned from getDBResultNum())
  • associative index (returned from getDBResultAssoc())
  • both indices (returned from getDBResultBoth()) by default is value returned from getDBResultAssoc(), that is, it returns an associative array
$with_index : string|null = null

indexes of the output array (for example it could be "id"), if specified the numeric value of the row is replaced with the value indicated in the index

Tags
Return values
int|null

the total of records found or null if the query is not valid or return empty results

getQueryDataValue()

Queries the DB and returns a specific value

public getQueryDataValue(string $sql[, string $name_col = '0' ][, int|null $pos_row = null ]) : string|null
Parameters
$sql : string

string containing the query to execute

$name_col : string = '0'

column name or numeric index (default 0)

$pos_row : int|null = null

position of the row starting from 0, if it is null or not indicated it reads the current value

Tags
Return values
string|null

indicated value of the row and column (by default it is the first column of current element)

getQueryFetchAssoc()

Queries the DB and returns an associative array containing the result

public getQueryFetchAssoc(string $sql[, int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$sql : string

string containing the query to execute

$pos_row : int|null = null

position of the row starting from 0, if it is null or not indicated it reads the current value

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

the associative array of the row indicated by $ pos_row or null if the result is not valid (e.g. if the index is outside the range of the table)

getQueryFetchRow()

Queries the DB and returns a row from the DB as an array having a numeric index

public getQueryFetchRow(string $sql[, int|null $pos_row = null ]) : array<string|int, mixed>|null
Parameters
$sql : string

string containing the query to execute

$pos_row : int|null = null

position of the row starting from 0, if it is null or not indicated it reads the current value

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

the array of the indicated row (having a numeric index) from $pos_row or null in which the result is not valid (e.g. if the index is outside the range of the table)

getTableColumnNames()

Returns an array containing the names of all columns of $tablename

public abstract getTableColumnNames(string $tablename) : array<string|int, string>
Parameters
$tablename : string

name of table

Tags
Return values
array<string|int, string>

an array with all fields or null if the table was not found

getTotalColumns()

Returns the number of columns of a previously executed query

public abstract getTotalColumns() : int|bool
Tags
Return values
int|bool

total of the columns that make up a table generated by the last query executed, FALSE in case of error

getTotalRows()

Reads the total of rows of a previously executed query

public abstract getTotalRows() : int|bool
Tags
Return values
int|bool

the total number of rows which make up a table generated by the last query executed or return false in case of error

info()

Interesting events.

public static info(string $message[, array<string|int, mixed> $context = [] ]) : void

Example: User logs in, SQL logs.

Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

insertRecord()

Make an entry in the DB

public abstract insertRecord(string|null $table, array<string, mixed> $items, array<string|int, mixed> $types) : bool
Parameters
$table : string|null

string containing the insert to execute

$items : array<string, mixed>

associative array containing FIELD_NAME => FIELD_VALUE of the table

$types : array<string|int, mixed>
Tags
Return values
bool

true if the insertion has been made

log()

Logs with an arbitrary level.

public static log(mixed $level, string $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$level : mixed
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

notice()

Normal but significant events.

public static notice(string $message[, array<string|int, mixed> $context = [] ]) : void
Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

prepare()

Prepares an SQL statement for execution

public abstract prepare(string $sql) : void
Parameters
$sql : string

string containing the query to execute

Tags
Return values
void

query()

Query the DB with SQL

public abstract query(string $sql[, array<string|int, mixed>|null $bind_param_values = null ][, array<string|int, string>|null $bind_param_types = null ]) : bool
Parameters
$sql : string

string containing the query to execute

$bind_param_values : array<string|int, mixed>|null = null

deferred values ​​of the query

$bind_param_types : array<string|int, string>|null = null

DB data types

Tags
Return values
bool

true if the query was successful

rollback()

Cancel all SQL operations performed after calling beginTransaction ()

public abstract rollback() : void
Tags
see
setAutoCommit()
see
beginTransaction()
see
commit()
Return values
void

setAutoCommit()

Set autocommit, as opposed to transitions, if true each operation is self-consistent and validated

public abstract setAutoCommit([bool $bool = true ]) : void
Parameters
$bool : bool = true

if is true the autocommit is running Note: if you use the transitions with you must be set to "false"

Tags
see
beginTransaction()
see
commit()
see
rollback()
Return values
void

setCurrentDBKey()

Set the current DB key NOTE: generally it is not necessary to use a DB key but it can be useful when there are multiple databases to manage

public setCurrentDBKey(string $dbkey) : bool
Parameters
$dbkey : string

DB key value to set

Tags
Return values
bool

true if the db key is valid

setDBTable()

Sets the name of the reference table

public setDBTable(string $table) : void
Parameters
$table : string

name of the main table to set

Tags
Return values
void

setExcludedControllers()

Delete some controllers derived from DB tables

public setExcludedControllers(array<string|int, mixed> $excluded_controllers) : void
Parameters
$excluded_controllers : array<string|int, mixed>

list of controllers derived from the DB to be excluded

Tags
Return values
void

setItemsRequired()

For POST, PUT, PATCH methods add some limitations as min or max value or a specific rule with regular expression

public setItemsRequired(array<string|int, mixed> $list) : void
Parameters
$list : array<string|int, mixed>

array contains per each table the relative limitations of single items

Tags
see
addItemRequired()

for a single table this is prefered method

example

$controller = CBaseController::getControllerName(); $rest = new $controller( ); // resource is the table/controller, the internal_code and the name are items with a limitated quantity of characters $rest->setItemRequired( ['student' => [ ['name'=>'age','min'=>16, 'max'=>35], ['name'=>'vote','min'=>18, 'max'=>30] ]] );

Return values
void

setRelatedFieldNames()

For the GET method, it replaces the index of the foreign key with the value of related field ($related_item_name) indicated in the related table ($table)

public setRelatedFieldNames(array<string|int, mixed> $list) : void
Parameters
$list : array<string|int, mixed>

array containing for each table the name of the field of the correlated table with which the index will have to be replaced

Tags
see
addRelatedFieldName()

for a single table this is prefered method

example

$controller_name = CBaseController::getControllerName(); $cntrl = new $controller_name( ); $cntrl->setRelatedFieldNames( ['_user'=>'email','*'=>'name'] );

Return values
void

setRelatedTables()

For the GET method, sets the name of the related tables to be presented for each single element of the current table

public setRelatedTables([array<string|int, mixed> $list = ['*'] ]) : void
Parameters
$list : array<string|int, mixed> = ['*']

array containing the list of related tables

Tags
see
setRelatedTables()

for a single table this is prefered method

example

$controller_name = CBaseController::getControllerName(); $cntrl = new $controller_name( ); $cntrl->setRelatedTables( ['comment','like'] );

Return values
void

setUser()

Set a user manager object (CUser or childs)

public setUser(CUser $user) : void
Parameters
$user : CUser

the user object

Tags
Return values
void

updateRecord()

Make an update in the DB

public abstract updateRecord(string|null $table, string $conditions, array<string, string> $items, array<string|int, string> $conditions_values, array<string|int, mixed> $types) : bool
Parameters
$table : string|null

string containing the update to be performed

$conditions : string

string containing the conditions of the update whose values ​​are replaced by a "?"

$items : array<string, string>

associative array containing FIELD_NAME => FIELD_VALUE of the table

$conditions_values : array<string|int, string>

array containing the values ​​of the conditions declared in $conditions

$types : array<string|int, mixed>
Tags
Return values
bool

true if the insertion has been made

warning()

Exceptional occurrences that are not errors.

public static warning(string $message[, array<string|int, mixed> $context = [] ]) : void

Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong.

Parameters
$message : string
$context : array<string|int, mixed> = []
Tags
Return values
void

_deleteDB()

Delete a record into a DB table

protected _deleteDB() : bool
Tags
throws
Exception

if the cancellation does not have any conditions or has not produced any effect

Return values
bool

true if the operation was successful, false in all other cases

_getCachedData()

Get a content in cache if exists

protected _getCachedData(string $cache_key[, string|null $table = null ][, bool $is_global = false ]) : mixed
Parameters
$cache_key : string

name of cache

$table : string|null = null

name of table (null or not declared is the implicit "current module")

$is_global : bool = false

true if is for all user

Tags
Return values
mixed

results of query or null if there is no data in the cache

_getCacheKey()

Get a content in cache if exists

protected _getCacheKey(string $key) : string
Parameters
$key : string

the content that uniquely identifies the cache

Tags
Return values
string

results of query or null if there is no data in the cache

_getContentFromDB()

Get a content in cache if exists

protected _getContentFromDB( $sql) : array<string|int, mixed>
Parameters
$sql :

SQL Composer Object contains the query to execute

Tags
Return values
array<string|int, mixed>

results of query or [] (emtpy array) if there is no data

_insertDB()

Insert a new record into a DB table

protected _insertDB() : bool
Tags
Return values
bool

true if the operation was successful, false in all other cases

_invalidateRelatedCache()

Invalid all cache related to current table and all related

protected _invalidateRelatedCache(string|null $table) : void
Parameters
$table : string|null

name of table to invalidate. If is null is the current table name

Tags
Return values
void

_realConnection()

Check the connection to the DB, if not there, try to connect

protected static _realConnection() : mixed
Tags
throws
Exception

if it can't connect to the DB

Return values
mixed

_revalidateHttpCache()

Invalidating the http cache N.B.: it is different from the internal data cache

protected _revalidateHttpCache() : void
Tags
Return values
void

_setCachedData()

Get a content in cache if exists

protected _setCachedData(string $cache_key, mixed $data[, string|null $table = null ][, bool $is_global = false ]) : void
Parameters
$cache_key : string

name of cache

$data : mixed

data to put in cache

$table : string|null = null

name of table (null or not declared is the implicit "current module")

$is_global : bool = false

true if is for all user

Tags
Return values
void

_updateDB()

Modify a record into a DB table

protected _updateDB([bool $all_params_required = true ]) : bool
Parameters
$all_params_required : bool = true
Tags
Return values
bool

true if the operation was successful, false in all other cases

changeCurrentConnectionKey()

Change the current db key (if you have more than on DB)

protected static changeCurrentConnectionKey(string|null $dbkey) : void
Parameters
$dbkey : string|null

the new dbkey for identify the DB connection if exists

Tags
throws
Exception

if $dbkey value is not a valid key

Return values
void

changeResult()

Change data and/or execute code before send data You can use: $this->method for distinguish the method used CRequest::setHtmlFrame(string $htmlframe) for change the html output file CRequest::getAttributes() for know other specific values from url

protected changeResult(array<string|int, mixed> &$result) : mixed
Parameters
$result : array<string|int, mixed>

final data before sending

Tags
Return values
mixed

convertDBType2BindType()

Converts from the type of the DB (INT, VARCHAR, FLOAT, BLOB) to the type of Bind (i, s, d, b)

protected convertDBType2BindType(string|null $db_type) : string|null
Parameters
$db_type : string|null

field db type

Tags
Return values
string|null

corresponding variable type for binding:

'i' corresponding variable has type integer 'd' corresponding variable has type double 's' corresponding variable has type string 'b' corresponding variable is a blob and will be sent in packets

doAfterOperation()

After an Insert, Modify or Delete, derived classes can use this method to perform additional operations

protected doAfterOperation(string $operation) : void
Parameters
$operation : string

type of operation ('insert', 'modify' or 'delete')

Tags
Return values
void

execPOST()

Manager of HTTP Method POST (insert)

protected execPOST() : array<string|int, mixed>
Tags
throws
Exception

if the element can't be inserted

Return values
array<string|int, mixed>

message result of insert operation

existsTableColumns()

Check if a named list of fields exists in a db table

protected abstract existsTableColumns(string|null $table, array<string|int, mixed> $names) : bool
Parameters
$table : string|null

name of table

$names : array<string|int, mixed>

list of field to check

Tags
Return values
bool

true if exists all fields

getConditions()

Import all necessary conditions based on filters and any additional conditions

protected getConditions() : string
Tags
Return values
string

string containing the conditions of an SQL query created with input filters and any other additional conditions

getConditionsByFilters()

Set the conditions according to the input filters

protected getConditionsByFilters() : string
Tags
Return values
string

string containing the conditions of an SQL query created with input filters

getConnectionError()

Set the current db key (if you have more than on DB)

protected static getConnectionError([string|null $dbkey = null ]) : int
Parameters
$dbkey : string|null = null

the new dbkey for identify the DB connection if exists

return int the id of error returned during the connection attempt

Tags
throws
Exception

if $dbkey value is not a valid key

Return values
int

getCurrentConnectionKey()

Get the current db key

protected static getCurrentConnectionKey() : string|null
Tags
Return values
string|null

key for identify the DB connection

isConnected()

Check the connection is active without error

protected static isConnected([string|null $dbkey = null ]) : bool
Parameters
$dbkey : string|null = null

valid key for identify the DB connection (if null or not declared the key is of current db) Is useful when you have more than one DB, in other case you don't need to use

Tags
Return values
bool

true if is connected

setDBColumns()

Returns an array containing the list of columns of a table each columns contains: - 'field' the name of column, - 'type' the type of column, - 'null' if accept the NULL DB value,

protected abstract setDBColumns(array<string|int, mixed> &$db_cols[, string $table = null ]) : void
Parameters
$db_cols : array<string|int, mixed>

the results to be returned

$table : string = null

the table to be sampled, if not specified or null, uses the previously set table

Tags
Return values
void

setItemsView()

protected setItemsView([array<string|int, mixed>|null $vitems = null ]) : void
Parameters
$vitems : array<string|int, mixed>|null = null

array containing for each table the name of the field of the correlated table with which the index will have to be replaced

Tags
Return values
void

setTable()

Set table name and keep all table fields with types NOTE: if $table have "-rel" the foreign key with id the keys will be replaced with their names

protected setTable(array<string|int, mixed> $db_tables,  $table) : void
Parameters
$db_tables : array<string|int, mixed>
$table :

table of DB or Controller

Tags
throws
Exception

if table or the field of order not exist

see
setMapFieldNames()
Return values
void

        

Search results