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

Source for file PMO_MyObject.php

Documentation is available at PMO_MyObject.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_MyMapObject class which implements
  4.  * the generic PMO_Object that represents a tuple in database.
  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. /** requires the interface */
  37. require_once(dirname(__FILE__).'/PMO_Object.php');
  38.  
  39.  
  40. /**
  41.  * This class describe a generic PMO_Object that represents a tuple
  42.  * from the database. It's a model class (the M part of the MVC design pattern)
  43.  * that enables you to create, read, update and delete data (CRUD).
  44.  * 
  45.  * This class can be extended by adding methods or fields
  46.  * and load dynamically via the PMO_Table mecanism
  47.  * 
  48.  * @package     PhpMyObject
  49.  * @subpackage PMO_Core
  50.  * @see             class_film_actor.php
  51.  * @tutorial     PhpMyObject/PhpMyObject.Quickstart.pkg#using.pmo_myObject a simple introduction to PMO_MyObject
  52.  * @tutorial     PhpMyObject/Manual.pkg#PMO_MyObject the manual chapter on PMO_MyObject
  53.  */ 
  54. class PMO_MyObject implements PMO_Object{
  55.     
  56.     /**
  57.      * holds the related PMO_Table object
  58.      * @var object 
  59.      */
  60.     protected $object_table;
  61.  
  62.     /**
  63.      * holds the object attributes
  64.      * @var PMO_MyArray 
  65.      */
  66.     protected $object_attribute ;
  67.  
  68.     /**
  69.      * boolean flag indicating if this object is a new one or
  70.      * has already been loaded through the {@link load()} method
  71.      * @var bool 
  72.      */
  73.     protected $object_new = TRUE;
  74.  
  75.     /**
  76.      * the constructor takes a PMO_Table argument and sets ifself up
  77.      * up so {@link factory()} will be able to create and return
  78.      * a PMO_Object
  79.      *
  80.      * @param object $table            a PMO_Table object
  81.      */
  82.     protected function __construct(PMO_Table $table){            
  83.         $this->object_table = $table;
  84.         $this->object_attribute = new PMO_MyArray();
  85.     }
  86.     
  87.     /**
  88.      * this internal factory will instanciate either PMO_Object based
  89.      * on an extended class in your class_loader directory or a generic
  90.      * PMO_MyObject object.
  91.      * 
  92.      * @param object $table        a PMO_Table object
  93.      * @return PMO_Object 
  94.      */
  95.     public static function internalfactory(PMO_Table $table){
  96.         $classname $table->getClassname()
  97.         if ($classname){
  98.             require_once(PMO_MyConfig::factory()->get('PMO_MyObject.CLASSPATH').PMO_MyConfig::factory()->get('PMO_MyObject.CLASS_FILENAME_PREFIX').$classname.'.php');
  99.             $object new $classname($table);
  100.         }else{
  101.             $object new PMO_MyObject($table);    
  102.         }
  103.         return $object;
  104.     }
  105.     
  106.     /**
  107.      * returns an object conforming to the database table schema
  108.      *
  109.      * This factory calls an internal factory that is used to load your own
  110.      * class if it exists in the class_loader directory and is referenced in
  111.      * the property $table_classname in the corresponding
  112.      * PMO_Table/PMO_Table_Classname.php file.
  113.      *
  114.      * @param string $tablename    the database table name to use
  115.      *                                         for building the PMO_Object object
  116.      * @return PMO_Object 
  117.      */
  118.     public static function factory($tablename){
  119.         $table PMO_MyTable::factory($tablename);
  120.         $object PMO_MyObject::internalfactory($table);
  121.         return $object;
  122.     }    
  123.         
  124.     /**
  125.      * Returns a reference to the PMO_Table object on which this object is based
  126.      *
  127.      * PMO_Table describe the structure of the table that was used to build
  128.      * the object data structure (column names, primary keys, ..)
  129.      * 
  130.      * @return PMO_Table 
  131.      * @throws Exception                if the PMO_Table is undefined
  132.      */
  133.     public function getTable(){
  134.             if(isset($this->object_table))
  135.                 return $this->object_table;
  136.         
  137.         throw new Exception("Error: Object is undefined");
  138.     }
  139.  
  140.     /**
  141.      * return the data structure of the PMO_Object
  142.      * array is attribute=>value
  143.      * 
  144.      * @return PMO_MyArray 
  145.      * @throws Exception
  146.      */
  147.     public function getObjectAttribute(){
  148.         if($this->object_attribute->count(0)
  149.             return $this->object_attribute;    
  150.         throw new Exception("Error: No data found in object");
  151.     }
  152.     
  153.     /**
  154.      * return an array wich contains
  155.      * all the names of attributes of PMO_Object
  156.      * 
  157.      * @return array 
  158.      * @throws Exception
  159.      */    
  160.     public function getListAttribute(){
  161.         $array new PMO_MyArray();
  162.         $attributes $this->getObjectAttribute();
  163.         foreach($attributes as $attributename=>$attributevalue)
  164.             $array->append($attributename);
  165.                 
  166.         return $array;
  167.     }
  168.  
  169.     /**
  170.     * retrieve the value of an attribute
  171.     * this function also cleans the escape chars
  172.     * with a simple stripslashes
  173.     * 
  174.     * @param sting $attribute 
  175.     * @throws Exception
  176.     */    
  177.     public function get($attribute){
  178.         if($this->object_attribute->offsetExists($attribute))
  179.             return stripslashes($this->object_attribute->offsetGet($attribute));
  180.                 
  181.         throw new Exception("Error: Attribute ".$attribute." doesn't exist");        
  182.     }
  183.  
  184.     public function fetch(){
  185.         return $this->object_attribute->fetch();
  186.     }
  187.     
  188.     /**
  189.     * Set the value of an attribute of the data structure
  190.     * this function already escape strange char with
  191.     * a  simple addslashes
  192.     * 
  193.     * data structure is an array
  194.     * attribute => value
  195.     *
  196.     * @param string $alias          column alias
  197.     * @param mixed $value          value to set the attribute to
  198.     * @throws Exception
  199.     * @return bool                      always TRUE
  200.     */    
  201.     public function set($attribute$value){
  202.         if($this->object_table->issetAttribute($attribute))
  203.             $this->object_attribute->offsetSet($attribute$value);
  204.         else
  205.             throw new Exception("Error: Attribute ".$attribute." value is undefined");
  206.         
  207.         return TRUE;
  208.     }
  209.     
  210.     /**
  211.      * export the data structure of an object in a xml format stream
  212.      *
  213.      * <code>
  214.      * <attributes>
  215.      *        <attributename>attributevalue</attributename>
  216.      * </attributes>
  217.      * </code>
  218.      */ 
  219.     public function toXml($encoding){
  220.         $out "<?xml version=\"1.0\" encoding=\"$encoding\"?>\r";
  221.         $out .= "<attributes>\r";
  222.  
  223.         foreach($this->object_attribute as $key=>$value)
  224.             $out .= "<{$key}>$value</{$key}>\r";
  225.  
  226.         $out .= "</attributes>\r";
  227.         return $out;
  228.     }
  229.  
  230.     /**
  231.      * alias of set function
  232.      *
  233.      * @param string $attributename 
  234.      * @param mixed $attributevalue 
  235.      * @see set()
  236.      */
  237.     public function __set($attributename$attributevalue{
  238.         $this->set($attributename$attributevalue);
  239.     }
  240.     
  241.     /**
  242.      * alias of get function
  243.      *
  244.      * @param string $attributename 
  245.      * @return mixed 
  246.      * @see get()
  247.      */
  248.     public function __get($attributename{
  249.         return $this->get($attributename);
  250.     }
  251.             
  252.     /**
  253.      * define the object as new, not already present in the database
  254.      *
  255.      * This flag is used to define when we do an insert or an update
  256.      * 
  257.      * @param bool $flag 
  258.      * @return void 
  259.      */
  260.     public function setNew($flag){
  261.         if(!is_bool($flag))
  262.             throw new Exception("Error: New flag should be a boolean");
  263.             
  264.         $this->object_new = $flag;    
  265.     }
  266.     
  267.     /**
  268.      * return the flag used to define if the objet is
  269.      * already present in database table
  270.      *
  271.      * @return bool 
  272.      */
  273.     public function getNew(){
  274.         return $this->object_new;
  275.     }
  276.     
  277.     /**
  278.      * Loads a data row from the database table using its primary key
  279.      * and fills the object attributes
  280.      *
  281.      * Primary key needs to be set beforehand
  282.      * 
  283.      * @throws Exception
  284.      * @return void 
  285.      */
  286.     public function load(){
  287.         if(PMO_MyConfig::factory()->get('PMO_MyMemCache.ACTIVE')){
  288.             PMO_MyMemCache::factory()->get($this);
  289.         }
  290.         $dbms PMO_MyDbms::factory();
  291.         $dbms->load($this);
  292.         $this->setNew(FALSE);    
  293.     }
  294.     
  295.     /**
  296.      * Deletes the corespondgng data row from the database table
  297.      *
  298.      * primary key needs to be set beforehand to delete the right tuple
  299.      * 
  300.      * @throws Exception
  301.      * @return void 
  302.      */
  303.     public function delete(){
  304.         if(PMO_MyConfig::factory()->get('PMO_MyMemCache.ACTIVE')){
  305.                 PMO_MyMemCache::factory()->delete($this);
  306.         }    
  307.         
  308.         $dbms PMO_MyDbms::factory();
  309.         $dbms->delete($this);
  310.     }
  311.     
  312.     /**
  313.      * Save this object into the corresponding database table
  314.      *
  315.      * If this is a new object, it will be inserted. Otherwise
  316.      * an update will be performed.
  317.      * 
  318.      * @throws Exception
  319.      */
  320.     public function save(){
  321.         if($this->getNew()){
  322.             $this->insert();
  323.         else {
  324.             $this->update();
  325.         }
  326.     }
  327.     
  328.     /**
  329.      * updates the corresponding data row into the database table
  330.      * 
  331.      * @throws Exception
  332.      */
  333.     public function update(){
  334.         $dbms PMO_MyDbms::factory();
  335.         $dbms->update($this);
  336.         if(PMO_MyConfig::factory()->get('PMO_MyMemCache.ACTIVE')){
  337.                 PMO_MyMemCache::factory()->set($this);
  338.         }        
  339.         $this->setNew(FALSE);
  340.     }
  341.  
  342.     /**
  343.      * inserts a new data row into the database table
  344.      *
  345.      * Fields must be filled beforehand.
  346.      * 
  347.      * @throws Exception
  348.      */    
  349.     public function insert(){
  350.         $dbms PMO_MyDbms::factory();
  351.         $dbms->insert($this);
  352.         if(PMO_MyConfig::factory()->get('PMO_MyMemCache.ACTIVE')){
  353.                 PMO_MyMemCache::factory()->set($this);
  354.         }            
  355.         $this->setNew(FALSE);
  356.     }
  357.     
  358. }
  359. ?>

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