Source for file PMO_MyTable.php
Documentation is available at PMO_MyTable.php
* This file contains the PMO_MyTable class which is used to hold the
* data structure of your database tables
* This file is part of the PhpMyObject project.
* For questions, help, comments, discussion, etc., please join our
* forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
* or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
* PhpMyObject is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* @author Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
* @link http://pmo.developpez.com/
* @since PhpMyObject v0.1x
* @copyright Copyright (C) 2007-2008 Nicolas Boiteux
* @license GPLv3 {@link http://www.gnu.org/licenses/gpl}
/** requires the interface */
require_once(dirname(__FILE__ ). '/PMO_Table.php');
* PMO_MyTable describes the data structure of your database tables.
* it is used to build the data structure of PMO_Object,
* retrieve the fields that are primary keys, use alias instead
* of the real name of columns.
* @todo provide some examples and link to tutorials
* this PMO_Table object name
* holds the table attributes represented by this object
* This array holds the table columns name and their properties and values.
* - Type the columns type: string, int, float, etc.
* - Null can the colun have null value: YES/NO
* - Key if value is PRI, the column is a primary key or part of it
* - Default value to default the column value with if none is provided
* - Extra is this column an auto_increment column? NULL/auto_increment
* - Perm the column permission: rw/r
* an array of PMO_Table objects
protected static $MAP = array();
* return a PMO_MyTable object from th PMO_MyTable directory or
* instanciates it if it does not exist yet.
* factory() first checks to see if the wanted PMO_Table has already
* been registered. If so it returns it immediately.
* Otherwise factory() will try to load the corresponding class from the
* PMO_MyTable/ directory.
* If it does not exist, factory() will finally
* instanciate a generic PMO_Mytable object, get the table structure
* and persist it on disk and register it for the next instanciation.
* @param string $tablename the table name
public static function factory($tablename){
if(isset (self::$MAP[$tablename]))
return self::$MAP[$tablename];
$classname = PMO_MyConfig::factory()->get('PMO_MyTable.CLASS_FILENAME_PREFIX'). $tablename;
self::$MAP[$tablename] = $table;
$table = new PMO_MyTable();
$table->populate($tablename);
self::$MAP[$tablename] = $table;
* populates table with the Dbms information
* @param string $tablename table name
private function populate($tablename){
$tmparray = PMO_MyDbms::factory()->getTableDesc($tablename);
foreach($tmparray as $attributevalue)
$this->set($attributevalue);
* retrieve the tablename of the object
* @throws Exception if the table name is not set
throw new Exception("Error: no tablename defined");
* retrieves the autoincrement field if it exists
if(! strcmp ($attributevalue['Extra'], 'auto_increment')){
* retrieves all the primary key of the object
* and returns them in an array
* Permission must be "rw" for the key to be returned.
* @throws Exception if the primary key is not defined
if($attributevalue['Key'] == 'PRI')
if($attributevalue['Perm'] == 'rw')
$array[] = $attributename;
throw new Exception("Error: primary key of table ". $this->getTableName(). " is undefined");
* sets the primary keys of an object with an array
* @param string $attributename a column name that is the table
* primary key or part of it
public function setPk($attributename){
* checks if the column is a primary key or not
* @param string $attributenane returns true if the attribute name
* is part of the table primary key
public function isPk($attributename){
* sets the table name of the object
* @param string $tablename
* retrieves an array that contains all the
* table columns names, e.g. [0]=>nameofcolumn
* @throws Exception if there is no attribute defined
throw new Exception("Error: table attributes are undefined");
* returns the permissions of an attribute: r=read, w=write
* @param string $attributename name of the attribute for which
* we want the permissions
* @throws Exception if the attribute is not defined
public function getPerm($attributename){
throw new Exception("Error: Perm of ". $attributename. " is undefined.");
* returns the type of an attribute: string, int, float
* @param string $attributename name of the attribute for which
public function getType($attributename){
* sets permission for an attribute (r=read, w=write)
* @param string $attributename attribute on which to set the permission
* @param string $value the permission to set, e.g. "r" or "rw"
* @throws Exception if the attribute is not set
public function setPerm($attributename, $value){
throw new Exception("Error: attribute ". $attributename. " doesn't exist");
* sets permissions for all attributes (r=read, w=write)
* @param string $value permission to set, e.g. "r" or "rw"
* returns an attribute in the datastructure of object
* array is attribute => value
* @param string $attributename attribute name for which we want
* @return array an array of attribute => value
* @throws Exception if $attributename does not exist
public function get($attribute){
throw new Exception("Error: Attribute ". $attribute. " of Table ". $this->getTableName(). " is undefined");
* checks if an attribute exists or not for the provided
* @param string $attributename
* return the class name used to instanciate a PMO_Object
* corresponding to this PMO_Table
* @return string|false the class name of false if not set
if(isset ($this->table_classname))
return $this->table_classname;
* sets the attribute "Field" with a value
* @param array $attributevalue
public function set(array $attributevalue){
$this->table_attribute[$attributevalue['Field']] = $attributevalue;
* sets the foreign key for an attribute
* @param string $attributename
* @throws Exception if the attribute does not exist
public function setFk($attributename, array $value){
if(isset ($this->table_attribute[$attributename]))
$this->table_attribute[$attributename]['Fk'] = $value;
throw new Exception("Error: attribute ". $attributename. " doesn't exist");
* returns the table foreign keys
* @todo this method returns the attribute value as a whole,
* should only return the foreign keys
* @return array|false array of attributes value or false if none exist
if(isset ($attributevalue['Fk']))
$array->offsetset($attributename, $attributevalue);
* creates a PMO_MyTable_xxx class at the first execution time, and
* this class extends the class PMO_MyTable,
* describe the data structure of tables, describe the primary keys,
* and the class name to used to instanciate
$cache .= "class PMO_MyTable_". $this->table_name. " extends PMO_MyTable{\n\n";
$cache .= "\tprotected \$table_name = '". $this->table_name. "';\n";
$cache .= "\tprotected \$table_attribute = Array(\n\t\t\t\t";
$tmp .= ',\''. $attribute. '\'=> Array('. "\n\t\t\t\t";
foreach($value as $attribute2=> $value2){
$tmp2 .= ',\''. $attribute2. '\'=> \''. $value2. '\''. "\n\t\t\t\t";
$cache .= "\tprotected \$table_classname = NULL;\n\n";
$handle = fopen($filename, "w");
|