PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_MyTable.php

Documentation is available at PMO_MyTable.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_MyTable class which is used to hold the
  4.  * data structure of your database tables
  5.  *
  6.  * This file is part of the PhpMyObject project.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * @package        PhpMyObject
  26.  * @subpackage PMO_Core
  27.  * @author        Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @link            http://pmo.developpez.com/
  29.  * @since        PhpMyObject v0.1x
  30.  * @version        $Revision: $
  31.  * @copyright    Copyright (C) 2007-2008 Nicolas Boiteux
  32.  * @license        GPLv3 {@link http://www.gnu.org/licenses/gpl}
  33.  * @filesource
  34.  *
  35.  */ 
  36.  
  37. /** requires the interface */
  38. require_once(dirname(__FILE__).'/PMO_Table.php');
  39.  
  40. /**
  41.  * PMO_MyTable describes the data structure of your database tables.
  42.  *
  43.  * it is used to build the data structure of PMO_Object,
  44.  * retrieve the fields that are primary keys, use alias instead
  45.  * of the real name of columns.
  46.  * 
  47.  * @todo            provide some examples and link to tutorials
  48.  * @package        PhpMyObject
  49.  * @subpackage PMO_Core
  50.  */
  51. class PMO_MyTable implements PMO_Table{
  52.  
  53.     /**
  54.     * this PMO_Table object name
  55.     *
  56.     * @var string 
  57.     * @access protected
  58.     */
  59.     protected $table_name;
  60.  
  61.     /**
  62.     * holds the table attributes represented by this object
  63.     * and their values
  64.     *
  65.     * This array holds the table columns name and their properties and values.
  66.     *
  67.     * Columns attributes
  68.     *
  69.     * - Type                  the columns type: string, int, float, etc.
  70.     * - Null                  can the colun have null value: YES/NO
  71.     * - Key                  if value is PRI, the column is a primary key or part of it
  72.     * - Default              value to default the column value with if none is provided
  73.     * - Extra              is this column an auto_increment column?  NULL/auto_increment
  74.     * - Perm                  the column permission: rw/r
  75.     *
  76.     * @var array 
  77.     * @access protected
  78.     */
  79.     protected $table_attribute = array();
  80.  
  81.     /**
  82.      * an array of PMO_Table objects
  83.      *
  84.      * @var array 
  85.      * @static
  86.      * @access protected
  87.      */
  88.     protected static $MAP array();
  89.     
  90.     /**
  91.      * return a PMO_MyTable object from th PMO_MyTable directory or
  92.      * instanciates it if it does not exist yet.
  93.      *
  94.      * factory() first checks to see if the wanted PMO_Table has already
  95.      * been registered. If so it returns it immediately.
  96.      *
  97.      * Otherwise factory() will try to load the corresponding class from the
  98.      * PMO_MyTable/ directory.
  99.      *
  100.      * If it does not exist, factory() will finally
  101.      * instanciate a generic PMO_Mytable object, get the table structure
  102.      * and persist it on disk and register it for the next instanciation.
  103.      * 
  104.      * @param string $tablename    the table name
  105.      * @return PMO_Table 
  106.      */
  107.     public static function factory($tablename){
  108.         if(isset(self::$MAP[$tablename]))
  109.             return self::$MAP[$tablename];
  110.         
  111.         $classname PMO_MyConfig::factory()->get('PMO_MyTable.CLASS_FILENAME_PREFIX').$tablename
  112.         $filename PMO_MyConfig::factory()->get('PMO_MyTable.CLASSPATH').$classname.".php";
  113.  
  114.         if(file_exists($filename))
  115.             require_once($filename);
  116.             
  117.         if(class_exists($classname)){
  118.             $table new $classname;
  119.             self::$MAP[$tablename$table;
  120.             return $table;
  121.         }else{
  122.             $table new PMO_MyTable();
  123.             $table->populate($tablename);
  124.             if(PMO_MyConfig::factory()->get('PMO_MyTable.CLASS_WRITE_ON_DISK')){
  125.                 $table->persist();
  126.                 return(PMO_MyTable::factory($tablename));
  127.             }else{
  128.                 self::$MAP[$tablename$table;
  129.                 return $table;
  130.             }
  131.         }
  132.     }
  133.     
  134.     /**
  135.      * populates table with the Dbms information
  136.      *
  137.      * @param string $tablename    table name
  138.      */    
  139.     private function populate($tablename){        
  140.             $this->setTableName($tablename);
  141.             $tmparray PMO_MyDbms::factory()->getTableDesc($tablename);
  142.             
  143.             foreach($tmparray as $attributevalue)
  144.                 $this->set($attributevalue);    
  145.     }
  146.     
  147.     /**
  148.      * retrieve the tablename of the object
  149.      * 
  150.      * @return string 
  151.      * @throws Exception            if the table name is not set
  152.      */
  153.     public function getTableName(){
  154.         if(isset($this->table_name))
  155.             return $this->table_name;
  156.         
  157.         throw new Exception("Error: no tablename defined");
  158.     }
  159.  
  160.     /**
  161.      * retrieves the autoincrement field if it exists
  162.      * and returns it
  163.     * 
  164.     * @return string|NULL
  165.     */
  166.     public function getAutoincrement(){
  167.         foreach($this->table_attribute as $attributename=>$attributevalue){
  168.             if(strcmp ($attributevalue['Extra']'auto_increment')){
  169.                 return $attributename;
  170.             }
  171.         }
  172.         return NULL;    
  173.     }            
  174.     
  175.     /**
  176.      * retrieves all the primary key of the object
  177.      * and returns them in an array
  178.      * 
  179.      * Permission must be "rw" for the key to be returned.
  180.      *
  181.      * @return array 
  182.      * @throws Exception            if the primary key is not defined
  183.      */    
  184.     public function getPk(){
  185.         foreach($this->table_attribute as $attributename=>$attributevalue){
  186.             if($attributevalue['Key'== 'PRI')
  187.                 if($attributevalue['Perm'== 'rw')
  188.                     $array[$attributename;
  189.         }
  190.  
  191.         if(isset($array))
  192.             return $array;
  193.         
  194.         throw new Exception("Error: primary key of table ".$this->getTableName()." is undefined");        
  195.     }    
  196.  
  197.     /**
  198.      * sets the primary keys of an object with an array
  199.      * 
  200.      * @param string $attributename    a column name that is the table
  201.      *                                             primary key or part of it
  202.      *  return void
  203.      */    
  204.     public function setPk($attributename){
  205.         $this->table_attribute[$attributename]['Key''PRI';
  206.     }
  207.     
  208.     /**
  209.      * checks if the column is a primary key or not
  210.      * 
  211.      * @param string $attributenane    returns true if the attribute name
  212.      *                                             is part of the table primary key
  213.      * @return boolean 
  214.      */
  215.     public function isPk($attributename){
  216.         if($this->table_attribute[$attributename]['Key'== 'PRI')
  217.             return TRUE;
  218.             
  219.         return FALSE;
  220.     }    
  221.     
  222.     /**
  223.      * sets the table name of the object
  224.      * 
  225.      * @param string $tablename 
  226.      * @return void 
  227.      */
  228.     public function setTableName($tablename){
  229.         $this->table_name = $tablename;
  230.     }
  231.     
  232.     /**
  233.      * retrieves an array that contains all the
  234.      * table columns names, e.g. [0]=>nameofcolumn
  235.      * 
  236.      * @return array 
  237.      * @throws Exception            if there is no attribute defined
  238.      */
  239.     public function getColumns(){
  240.         if(count($this->table_attribute1)
  241.             throw new Exception("Error: table attributes are undefined");
  242.         
  243.         foreach($this->table_attribute as $name=>$value)
  244.             $array[$name;
  245.             
  246.             return $array;
  247.     }
  248.  
  249.     /**
  250.      * returns the permissions of an attribute: r=read, w=write
  251.      * 
  252.      * @param string $attributename        name of the attribute for which
  253.      *                                                 we want the permissions
  254.      * @return string 
  255.      * @throws Exception                        if the attribute is not defined
  256.      */
  257.     public function getPerm($attributename){
  258.         if(isset($this->table_attribute[$attributename]['Perm']))
  259.             return $this->table_attribute[$attributename]['Perm'];
  260.             
  261.         throw new Exception("Error: Perm of ".$attributename." is undefined.");
  262.     }
  263.     
  264.     /**
  265.      * returns the type of an attribute: string, int, float
  266.      * 
  267.      * @param string $attributename        name of the attribute for which
  268.      *                                                 we want the type
  269.      * @return string 
  270.      */
  271.     public function getType($attributename){
  272.         return $this->table_attribute[$attributename]['Type'];
  273.     }
  274.     
  275.     /**
  276.      * sets permission for an attribute (r=read, w=write)
  277.      *
  278.      * @param string $attributename        attribute on which to set the permission
  279.      * @param string $value                    the permission to set, e.g. "r" or "rw"
  280.      * @throws Exception                        if the attribute is not set
  281.      */
  282.     public function setPerm($attributename$value){
  283.         if(isset($this->table_attribute[$attributename]))
  284.             $this->table_attribute[$attributename]['Perm'$value;
  285.         else
  286.             throw new Exception("Error: attribute ".$attributename." doesn't exist");
  287.     }
  288.  
  289.     /**
  290.      * sets permissions for all attributes (r=read, w=write)
  291.      *
  292.      * @param string $value            permission to set, e.g. "r" or "rw"
  293.      */
  294.     public function setPermForAll($value){
  295.         foreach($this->table_attribute as $attributename=>$array)
  296.             $this->table_attribute[$attributename]['Perm'$value;
  297.     }
  298.         
  299.     /**
  300.     * returns an attribute in the datastructure of object
  301.     * array is attribute => value
  302.     * 
  303.     * @param string $attributename  attribute name for which we want
  304.     *                                           the properties
  305.     * @return array                      an array of attribute => value
  306.     * @throws Exception                  if $attributename does not exist
  307.     */    
  308.     public function get($attribute){
  309.         if(isset($this->table_attribute[$attribute]))
  310.             return $this->table_attribute[$attribute];
  311.         
  312.         throw new Exception("Error: Attribute ".$attribute." of Table ".$this->getTableName()." is undefined");
  313.     }
  314.     
  315.     /**
  316.      * checks if an attribute exists or not for the provided
  317.      * attribute
  318.      * 
  319.      * @param string $attributename 
  320.      * @return boolean 
  321.      */
  322.     public function issetAttribute($attributename){        
  323.         if(isset($this->table_attribute[$attributename]))
  324.             return TRUE;
  325.         
  326.         return FALSE;
  327.     }    
  328.     
  329.     /**
  330.      * return the class name used to instanciate a PMO_Object
  331.      * corresponding to this PMO_Table
  332.      * 
  333.      * @return string|false       the class name of false if not set
  334.      */
  335.     public function getClassname(){
  336.         if(isset($this->table_classname))
  337.             return $this->table_classname;
  338.             
  339.         return FALSE;
  340.     }
  341.  
  342.     /**
  343.      * sets the attribute "Field" with a value
  344.      * 
  345.      * @param array $attributevalue 
  346.      * @return void 
  347.      */    
  348.     public function set(array $attributevalue){
  349.         $this->table_attribute[$attributevalue['Field']] $attributevalue;
  350.     }
  351.  
  352.     /**
  353. /**
  354.      * sets the foreign key for an attribute
  355.      *
  356.      * @param string $attributename 
  357.      * @param array $value 
  358.      * @throws Exception            if the attribute does not exist
  359.      */
  360.     public function setFk($attributenamearray $value){
  361.         if(isset($this->table_attribute[$attributename]))
  362.             $this->table_attribute[$attributename]['Fk'$value;
  363.         else
  364.             throw new Exception("Error: attribute ".$attributename." doesn't exist");
  365.     }
  366.     
  367.     /**
  368.      * returns the table foreign keys
  369.      * 
  370.      * @todo this method returns the attribute value as a whole,
  371.      *             should only return the foreign keys
  372.      * @return array|false       array of attributes value or false if none exist
  373.      * @throws Exception
  374.      */
  375.     public function getFk(){
  376.         $array new PMO_MyArray();
  377.         foreach($this->table_attribute as $attributename=>$attributevalue){
  378.                 if(isset($attributevalue['Fk']))
  379.                     $array->offsetset($attributename$attributevalue);
  380.         }
  381.         
  382.         return $array;    
  383.     }
  384.     
  385.     /**
  386.     * creates a PMO_MyTable_xxx class at the first execution time, and
  387.     * flushes it to the disk
  388.     * this class extends the class PMO_MyTable,
  389.     *
  390.     * describe the data structure of tables, describe the primary keys,
  391.     * and the class name to used to instanciate
  392.     * the PMO_Object
  393.     * 
  394.     * @return bool 
  395.     */
  396.     public function persist(){
  397.  
  398.         $cache "<?php \n";
  399.         $cache .= "class PMO_MyTable_".$this->table_name." extends PMO_MyTable{\n\n";
  400.         
  401.         $cache .= "\tprotected \$table_name = '".$this->table_name."';\n";        
  402.         $cache .= "\tprotected \$table_attribute = Array(\n\t\t\t\t";
  403.             
  404.         $tmp "";
  405.         foreach($this->table_attribute as $attribute=>$value){
  406.             $tmp .= ',\''.$attribute.'\'=> Array('."\n\t\t\t\t";
  407.             $tmp2 "";
  408.             foreach($value as $attribute2=>$value2){
  409.                 $tmp2 .= ',\''.$attribute2.'\'=> \''.$value2.'\''."\n\t\t\t\t";            
  410.             }
  411.             $tmp .= substr($tmp21strlen($tmp2));
  412.             $tmp .= ")\n\t\t\t\t";
  413.         }
  414.         
  415.         $cache .= substr($tmp1strlen($tmp));
  416.         $cache .=");\n\n";
  417.                 
  418.         $cache .= "\tprotected \$table_classname = NULL;\n\n";
  419.         
  420.         $cache .="}\n";
  421.         $cache .= "?>";
  422.         
  423.         $filename PMO_MyConfig::factory()->get('PMO_MyTable.CLASSPATH').PMO_MyConfig::factory()->get('PMO_MyTable.CLASS_FILENAME_PREFIX').$this->table_name.'.php';
  424.         $handle fopen($filename"w");
  425.         if(!fwrite($handle$cachestrlen($cache)))
  426.             throw new Exception("Error: PMO_MyTable ".$this->getTableName()." can not be write in ".PMO_MyConfig::factory()->get('PMO_MyTable.CLASSPATH')." directory");
  427.         fclose($handle);
  428.         
  429.         return TRUE;
  430.     }
  431. }
  432.  
  433. ?>

Documentation generated on Wed, 15 Oct 2008 09:17:53 -0400 by phpDocumentor 1.4.1