L'objet "INFOS"
Below are the properties and methods specific to the object
Infos
:
Properties
The class
Infos
inherits properties and methods of the class
Listing
, so it's
useless to remind them here.
$data
ARRAY — Array containing data of the entry found.
$loaded
BOOLEAN — TRUE if database has already been read (to check before 'update' or 'insert' -> see method save()
).
Methods
__construct()
GETTING content of an entry of an SQL table: Initialization
__construct(
STRING $table, OBJECT $pdoInstance = false
)
Parameters
STRING |
$table |
SQL table name |
OBJECT |
$pdoInstance |
Predefined PDO instance (optional) |
setTable()
Definition of the table where to find / add an entry.
setTable(
STRING $table
)
Parameters
STRING |
$table |
SQL table name |
getTable()
Get the current table name.
getTable() : STRING
Returns
STRING — Name of the current table.
loadInfos()
Load an entry according to a basic filter. Throws an error if several entries found (and not only one).
loadInfos(
STRING $filtreKey, STRING $filtreVal,
BOOLEAN $withFK = true, BOOLEAN $decodeJson = true, BOOLEAN $parseDatesJS = true
)
Parameters
STRING |
$filtreKey |
Filter column name |
STRING |
$filtreVal |
Filter value for the entry |
BOOLEAN |
$withFK |
TRUE to get JOINED data (default TRUE) |
BOOLEAN |
$decodeJson |
TRUE to automatically decode fileds containing JSON. FALSE to get JSON fields as STRING (default TRUE) |
BOOLEAN |
$parseDatesJS |
TRUE to format dates as ISO 8601 for javascript (default TRUE) |
isLoaded()
Check if data has been loaded.
isLoaded() : BOOLEAN
Returns
BOOLEAN — TRUE if data has already been loaded from database.
getInfo()
Get the data of a specified column.
getInfo(
STRING $column = "*"
) : MIXED
Parameters
STRING |
$column |
Name of the column which we want the data |
Returns
MIXED — Value of the column, FALSE if no data found.
getManyInfos()
Get several data of specified columns, or every data of the entry in memory.
getManyInfos(
ARRAY|STRING $columns = "*"
) : ARRAY
Parameters
ARRAY|STRING |
$columns |
Columns names which we want the data, in an array, or a string comma-separated (default '*' -> all columns) |
Returns
ARRAY — An array containing asked data (empty if no data found).
countInfos()
Counts the number of fields of the entry in memory.
countInfos() : INT
Returns
INT — The number of fields (columns).
setInfo()
Add / Update a field for the entry in memory.
(
STRING $key, STRING $val
)
Parameters
STRING |
$key |
The column name |
STRING |
$val |
The value for the column |
setManyInfos()
Update several fields of an entry in memory at once (allows to check integrity of the entry, in term of columns).
setManyInfos(
ARRAY $newInfos,
BOOLEAN $allowAddCol = false, BOOLEAN $checkMissing = false, BOOLEAN $forceID = false
)
Parameters
ARRAY |
$newInfos |
An array containing the new values for the columns of the entry |
BOOLEAN |
$allowAddCol |
TRUE to ignore extra columns, FALSE to allow the addition of inexistent columns (default FALSE) |
BOOLEAN |
$checkMissing |
TRUE to check if all columns are defined $newInfos (throws an error), FALSE to let MySQL feed missing values with defaults (default FALSE) |
BOOLEAN |
$forceID |
TRUE to force the definition of the column "id", FALSE to ignore it and let MySQL do its auto-increment (default FALSE) |
save()
SAVE an entry of the current table in database.
save(
STRING $filterKey = 'id', STRING $filterVal = 'this',
BOOLEAN $autoAddCol = true, BOOLEAN $autoDate = true
) : STRING
Parameters
STRING |
$filterKey |
Name of the column used to identify the entry (default 'id') |
STRING |
$filterVal |
The identifier value (default 'this' -> the current entry) |
BOOLEAN |
$autoAddCol |
TRUE to agg the column(s) if it (they) doesn't exist |
BOOLEAN |
$autoDate |
TRUE to update the field of last modification with the current date, (or creation date in case of an INSERT, if the column is present. (default TRUE) |
Returns
STRING — The type of the SQL request which has just been used to save ('UPDATE', ou 'INSERT')
delete()
Delete one (or several) entry(ies) from the database.
delete(
STRING $filterKey = 'id', STRING $filterVal = 'this', STRING $filtrePlus = null
) : INT
Parameters
STRING |
$filterKey |
Name of the column used to identify the entry (default 'id') |
STRING |
$filterVal |
The identifier value (default 'this' -> the current entry) |
STRING |
$filtrePlus |
Additionnal filter for the SQL request allowing best identification of the entry (optionnel) |
Returns
INT — Number of entries actually deleted.
colExists()
STATIC Check if a column exists in the current table.
Infos::colExists(
STRING $table, STRING $colName
) : BOOLEAN
Parameters
STRING |
$table |
Name of the table |
STRING |
$colName |
Name of the column |
Returns
BOOLEAN — TRUE if the column exists, FALSE otherwise.
colIndex_isUnique()
STATIC Check if a column has a UNIQUE index (i.e. if it can share the same value for several entries).
Infos::colIndex_isUnique(
STRING $table, STRING $colName
) : BOOLEAN
Parameters
STRING |
$table |
Name of the table |
STRING |
$colName |
Name of the column to check |
Returns
BOOLEAN — TRUE if the column has a UNIQUE index, FALSE otherwise.
addNewCol()
STATIC Add a column in a table of the database.
Infos::addNewCol(
STRING $table, STRING $colName, STRING $colType = 'VARCHAR(64)', STRING $defaultVal = ''
) : BOOLEAN
Parameters
STRING |
$table |
Name of the table |
STRING |
$colName |
Name of the new column |
STRING |
$colType |
Type of the column to add (default "VARCHAR(64)") |
STRING |
$defaultVal |
Default value for the column (optional, and useless for type "TEXT") |
Returns
BOOLEAN — TRUE if success, FALSE if error.
removeCol()
STATIC Remove a column from a table of the database. Warning: incompatible with SQLite at the moment.
Infos::removeCol(
STRING $table, STRING $colName
) : BOOLEAN
Parameters
STRING |
$table |
Name of the table |
STRING |
$colName |
Name of the column to remove |
Returns
BOOLEAN — TRUE if success, FALSE if error.
createMissingCols()
Check if all fields exist, otherwise create the columns on the fly.
createMissingCols()
autoAddCol()
Add a column to the current table, with automatic choice of the column type.
autoAddCol(
STRING $colName, STRING $val
) : BOOLEAN
Parameters
STRING |
$colName |
Name of the column |
STRING |
$val |
Value of the column (in order to auto-check the value type) |
Returns
BOOLEAN — TRUE if success, FALSE if fail.
Exceptions
Here is a list of exceptions which can be thrown when using Infos object, and their signification.
Exceptions for __construct()
Infos::__construct() : missing table name
This means that the table used to find the entry is missing. Choose a table in the database, and give its name to the
parameter $table
of new Infos()
.
Exceptions for setTable()
Infos::setTable() : Table '$table' doesn't exists
This means that the specified table doesn't exists in database.
Choose a table in the database, and give its name to parameter $table
of setTable()
.
Exceptions for loadInfos()
Infos::loadInfos() : Several entries ($number) found for '$filtreKey = $filtreVal'! Please refine your filter
This means that more than one entry were found for the search. The 'Infos' object have been designed to work on a precise
entry of a table, it throw an exception when the result is multiple.
Choose a more precise filtering to find the entry, changing the parameters $filtreKey
and $filtreVal
for loadInfos()
.
Exceptions for getInfo()
Infos::getInfo() : Missing column name
This means that the column name is missing, and it's needed to get a specific value of the current entry.
Choose a column in the current entry's table, and give its name to parameter $column
of getInfo()
.
Exceptions for setManyInfos()
Infos::setManyInfos() : 'newInfos' must be an array ($type found)
This means that the variable type of parameter $newInfos
is not an array. To modify the current entry with
method setManyInfos()
, you must give an associative array, which contain the values to change. Each array's key
being the column name, and its value being the new value for the column.
Infos::setManyInfos() : missing $number columns in array 'newInfos', compared to current table ('$table'). List of missing columns: json_encode($missingRows)
When parameter $checkMissing
of setManyInfos()
is True
, the method will perform a verficiation
on the list of the $newInfos
array's keys to check if some are missing.
This exception is thrown when some columns are missing in the array, and give a list of those missing columns.
Exceptions for save()
Infos::save() : Duplicate entry for `$key`="$val" in table '$table'.
This means that one of the values of the entry to be saved already exists in the database, because of a column that has a unique index.
Change the new value of the entry before calling save()
.
Infos::save() : table '$table' -> $msg.
Infos::save() : '.$error
These two exceptions are thrown when PDO encounter an error. Detail of this error is specified in the message.
Exceptions for addNewCol()
Infos::addNewCol() : Missing table name
This means that the table's name is missing to add a column into.
Infos::addNewCol() : Missing column name
This means that the column's name to add in table is missing.
Infos::addNewCol() : This column already exists
This means that the column is already present in the table. Choose another name for the column.
Exceptions for removeCol()
Infos::removeCol() : SQLite3 limitation: you can't drop a column from a table with 'ALTER TABLE' statement.
This exception is thrown when PDO driver 'sqlite' is in use. Because of a limitation in ALTER functions in SQLite,
DROP being unavailable, this method is at the moment unusable. You'll have to do this operation "by hand" on and SQLite console.
Other exceptions may appear, they are probably thrown by PDO himself.