MySQL Object. More...
Public Member Functions | |
__construct (PDO $pdo, $id=null) | |
constructor | |
getTable () | |
getColumns () | |
getStructure () | |
Get the complete table structure. | |
Static Public Member Functions | |
static | all (PDO $pdo, array $search_params=array(), array $options=array()) |
Obtain a collection of MySQL Objects optionaly filtered by $search_params and $options parameters. | |
Protected Member Functions | |
_init ($statement) | |
_getTableStructure ($table) | |
Get the structure from any table name. | |
Static Protected Member Functions | |
static | _sanitizeTablename ($table) |
Sanitize the tablename. | |
static | _generateSelectQuery ($tablename, array $columns=array(), array $search_params=array(), array $options=array()) |
Generates a `SELECT` query according to the given parameters. | |
static | _generateInsertQuery ($tablename, array $columns) |
Generate a SQL `INSERT` query. | |
static | _generateUpdateQuery ($tablename, array $columns, array $search_params=array()) |
Generate a SQL `UPDATE` query. | |
static | _generateDeleteQuery ($tablename, array $search_params=array()) |
Generate a SQL `DELETE` query. | |
static | _generateWhereClause (array $search_params) |
Generates a query `WHERE` clause. | |
static | _generateOptionClause (array $options) |
Generates a query `GROUP BY`, `ORDER BY` and `LIMIT` clauses. | |
Protected Attributes | |
$_table | |
Table name. | |
$_structure | |
Table structure (describe result) |
MySQL Object.
Helper class to map a MySQL row record into an axModel object. Basicaly, this class is just an axModel class capable of understanding a MySQL table structure and to translate it to generic CRUD queries.
Definition at line 22 of file axMySQLObject.class.php.
axMySQLObject::__construct | ( | PDO $ | pdo, |
$ | id = null |
||
) |
constructor
You may pass an $id to get the row data directly so no further calls to axModel::retrieve is necessary.
PDO | $pdo | The database connection instance |
string | $tablename | The MySQL table name |
string | $id | [optional] [default null ] The ID of the mysql row to match |
Reimplemented from axBaseModel.
Definition at line 88 of file axMySQLObject.class.php.
static axMySQLObject::_generateDeleteQuery | ( | $ | tablename, |
array $ | search_params = array() |
||
) | [static, protected] |
Generate a SQL `DELETE` query.
string | $tablename | |
array | $columns |
Definition at line 361 of file axMySQLObject.class.php.
static axMySQLObject::_generateInsertQuery | ( | $ | tablename, |
array $ | columns | ||
) | [static, protected] |
Generate a SQL `INSERT` query.
string | $tablename | |
array | $columns |
Definition at line 341 of file axMySQLObject.class.php.
static axMySQLObject::_generateOptionClause | ( | array $ | options | ) | [static, protected] |
Generates a query `GROUP BY`, `ORDER BY` and `LIMIT` clauses.
See axMySQLObject::all() description for more details about the $options
structure. Will return an empty string if the $options
parameter is empty.
array | $search_params |
Definition at line 404 of file axMySQLObject.class.php.
static axMySQLObject::_generateSelectQuery | ( | $ | tablename, |
array $ | columns = array() , |
||
array $ | search_params = array() , |
||
array $ | options = array() |
||
) | [static, protected] |
Generates a `SELECT` query according to the given parameters.
See axMySQLObject::all() for more details about $search_params
and $options
parameters format.
string | $tablename | |
array | $columns | [optional] [default array() ] The column names |
array | $search_params | [optional] [default array() ] |
array | $options | [optional] [default array() ] |
InvalidArgumentException | If the $tablename parameter is empty |
Definition at line 315 of file axMySQLObject.class.php.
static axMySQLObject::_generateUpdateQuery | ( | $ | tablename, |
array $ | columns, | ||
array $ | search_params = array() |
||
) | [static, protected] |
Generate a SQL `UPDATE` query.
string | $tablename | |
array | $columns |
Definition at line 351 of file axMySQLObject.class.php.
static axMySQLObject::_generateWhereClause | ( | array $ | search_params | ) | [static, protected] |
Generates a query `WHERE` clause.
See axMySQLObject::all() for more details about $search_params
parameter format. Will return an empty string if the $search_params
parameter is empty.
array | $search_params |
Definition at line 373 of file axMySQLObject.class.php.
axMySQLObject::_getTableStructure | ( | $ | table | ) | [protected] |
Get the structure from any table name.
The structure is parsed from the MySQL DESCRIBE (DESC) query result. Will return true in case of success, false otherwise.
string | $table | The tablename |
InvalidArgumentException | If $table parameter is invalid or empty |
Definition at line 267 of file axMySQLObject.class.php.
axMySQLObject::_init | ( | $ | statement | ) | [protected] |
Initialize a statement.
This method is intended to create the `PDOStatement` objects used by CRUD methods (create, retrieve, update, delete).
A statement is identified by its name:
string | $statement | The statement name |
Reimplemented from axBaseModel.
Definition at line 39 of file axMySQLObject.class.php.
static axMySQLObject::_sanitizeTablename | ( | $ | table | ) | [static, protected] |
Sanitize the tablename.
Escape properly the given tablename Example:
$table = self::_sanitizeTablename('database.table'); echo $table; // will display `database`.`table`
string | $table |
Definition at line 299 of file axMySQLObject.class.php.
static axMySQLObject::all | ( | PDO $ | pdo, |
array $ | search_params = array() , |
||
array $ | options = array() |
||
) | [static] |
Obtain a collection of MySQL Objects optionaly filtered by $search_params
and $options
parameters.
The last $object
parameter can be either an axModel instance or a valid table name. axMySQLOjbect::all() will generate a generic SQL `SELECT` query to fetch any row that match the $search_params
and $options
conditions (if any). An axPDOStatement instance is returned in case of success, which lets you iterate over the collection. Each collection item is a valid instance of axModel you can obviously perform any CRUD operation on.
Usage:
// Retrive all items in the `mydb`.`users` table axMySQLObject::all($pdo, array(), array(), 'mydb.users'); // Retrive all items in the `mydb`.`users` table having the `mail` field set to `foo@bar.com` axMySQLObject::all($pdo, array('mail' => 'foo@bar.com'), array(), 'mydb.users'); // Retrieve all items in the `mydb`.`users` and order them by login name axMySQLObject::all($pdo, array(), array('order by' => 'login'), 'mydb.users'); // Retrieve all items in the `mydb`.`users` having a `privilege_level` higher than 5 axMySQLObject::all($pdo, array('privilege_level >=' => 5), array(), 'mydb.users'); // Retrive all users using a valid user instance $user = new User($pdo, $id); // User class implements axModel axMySQLObject::add($pdo, array(), array(), $user);
$search_params
cannot be listed twice, even if you specify the operator. Example: // The following call will result as a query error axMySQLObject::all($pdo, array('privilege_level >=' => 5, 'privilege_level <=' => 10), array(), 'mydb.users');
// Retrieve all items in the `mydb`.`users` having a `privilege_level` higher than 5 and lower than 10 axMySQLObject::all($pdo, array('privilege_level BETWEEN' => array(5,10)), array(), 'mydb.users');
The $options
parameters may have the following parameters (in any order):
$option
parameter will be ignored. Any incorrect option parameter will also be ignored. Example: $options = array( 'group by' => array('colA', 'colB'), 'order by' => 'colC', 'order by type' => 'DESC', 'limit' => array(0,10) );
Will return false if the generated query execution fails.
PDO | $pdo | |
array | $search_params | The search parameters |
array | $options | The query options (group by, order by and limits) |
mixed | $object | Either a tablename or a valid axObject instance (will trigger an E_USER_WARNING if this parameter is invalid) |
InvalidArgumentException | If the fourth argument is not a string or instance of axModel |
Implements axModel.
Definition at line 187 of file axMySQLObject.class.php.
Get the record's original table columns.
Implements axModel.
Definition at line 244 of file axMySQLObject.class.php.
Get the complete table structure.
Definition at line 252 of file axMySQLObject.class.php.
Get the record's original table name.
Implements axModel.
Definition at line 237 of file axMySQLObject.class.php.
array axMySQLObject::$_structure [protected] |
Table structure (describe result)
Definition at line 34 of file axMySQLObject.class.php.
string axMySQLObject::$_table [protected] |
Table name.
Definition at line 28 of file axMySQLObject.class.php.