Axiom (v1.2.0)

A lightweight PHP framework
axMySQLObject Class Reference

MySQL Object. More...

+ Inheritance diagram for axMySQLObject:

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)

Detailed Description

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.

Warning:
Only tables with strictly one attribute as primary key can be used with this class. For more complex object types, you should describe your own behavior by implementing `axModel`.
Author:
Delespierre
License:
http://www.gnu.org/licenses/lgpl.html Lesser General Public Licence version 3

Definition at line 22 of file axMySQLObject.class.php.


Constructor & Destructor Documentation

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.

Parameters:
PDO$pdoThe database connection instance
string$tablenameThe 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.


Member Function Documentation

static axMySQLObject::_generateDeleteQuery ( tablename,
array $  search_params = array() 
) [static, protected]

Generate a SQL `DELETE` query.

Todo:
To be implemented
Parameters:
string$tablename
array$columns
Returns:
string

Definition at line 361 of file axMySQLObject.class.php.

static axMySQLObject::_generateInsertQuery ( tablename,
array $  columns 
) [static, protected]

Generate a SQL `INSERT` query.

Todo:
To be implemented
Parameters:
string$tablename
array$columns
Returns:
string

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.

Parameters:
array$search_params
Returns:
string

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.

Parameters:
string$tablename
array$columns[optional] [default array()] The column names
array$search_params[optional] [default array()]
array$options[optional] [default array()]
Exceptions:
InvalidArgumentExceptionIf the $tablename parameter is empty
Returns:
string

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.

Todo:
To be implemented
Parameters:
string$tablename
array$columns
Returns:
string

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.

Parameters:
array$search_params
Returns:
string

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.

Parameters:
string$tableThe tablename
Exceptions:
InvalidArgumentExceptionIf $table parameter is invalid or empty
Returns:
boolean

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:

  • create : for record creation
  • retrieve : for record retrieving
  • update : for record update
  • delete : for deleting a record
Parameters:
string$statementThe statement name
Returns:
PDOStatement

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`
Parameters:
string$table
Returns:
string

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);
Warning:
A field listed in $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');
Note:
you may use the `BETWEEN` operator to restrict results in a given range. Example:
 // 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 `WHERE` clause generation engine will produce prepared statements compliant string (see http://php.net/manual/en/class.pdostatement.php). You should not use invalid replacement values like sub-queries or string containing SQL keywords like 'xxx AND yyy'.

The $options parameters may have the following parameters (in any order):

  • group by : any string or array of strings value describing a field or a list of fields
  • limit : an integer or an array containing 2 integers describing the limit bounds
  • order by : any string or array of strings value describing a field or a list of fields
  • order by type : `ASC` or `DESC` Any other key for the $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.

Warning:
All parameters are mandatory.
Parameters:
PDO$pdo
array$search_paramsThe search parameters
array$optionsThe query options (group by, order by and limits)
mixed$objectEither a tablename or a valid axObject instance (will trigger an E_USER_WARNING if this parameter is invalid)
Exceptions:
InvalidArgumentExceptionIf the fourth argument is not a string or instance of axModel
Returns:
axPDOStatementIterator

Implements axModel.

Definition at line 187 of file axMySQLObject.class.php.

Get the record's original table columns.

Returns:
array

Implements axModel.

Definition at line 244 of file axMySQLObject.class.php.

Get the complete table structure.

Returns:
array

Definition at line 252 of file axMySQLObject.class.php.

Get the record's original table name.

Returns:
string

Implements axModel.

Definition at line 237 of file axMySQLObject.class.php.


Field Documentation

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.


The documentation for this class was generated from the following file:
 All Data Structures Files Functions Variables