b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _class_dataobject.php

Documentation is available at _class_dataobject.php

  1. <?php
  2. /**
  3.  * Data Object Base Class
  4.  *
  5.  * "data objects by fplanque" :P
  6.  *
  7.  * b2evolution - {@link http://b2evolution.net/}
  8.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  9.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  10.  *
  11.  * @package evocore
  12.  */
  13. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  14.  
  15. /**
  16.  * Data Object Base Class
  17.  *
  18.  * This is typically an abstract class, useful only when derived.
  19.  *
  20.  * @package evocore
  21.  * @version beta
  22.  * @abstract
  23.  */
  24. class DataObject
  25. {
  26.     /**
  27.      * Unique ID of object in database
  28.      *
  29.      * Please use get/set functions to read or write this param
  30.      *
  31.      * @var int 
  32.      * @access protected
  33.      */
  34.     var    $ID = 0;        // This will be the ID in the DB
  35.  
  36.     /**#@+
  37.      * @access private
  38.      */
  39.     var    $dbtablename;
  40.     var $dbprefix;
  41.     var $dbIDname;
  42.     var $dbchanges array();
  43.     /**#@-*/
  44.  
  45.     /**
  46.      * Constructor
  47.      *
  48.      * {@internal DataObject::DataObject(-) }}
  49.      *
  50.      * @param string Name of table in database
  51.      * @param string Prefix of fields in the table
  52.      * @param string Name of the ID field (including prefix)
  53.      */
  54.     function DataObject$tablename$prefix ''$dbIDname 'ID' )
  55.     {
  56.         $this->dbtablename $tablename;
  57.         $this->dbprefix $prefix;
  58.         $this->dbIDname $dbIDname;
  59.     }
  60.  
  61.     /**
  62.      * Records a change that will need to be updated in the db
  63.      *
  64.      * {@internal DataObject::dbchange(-) }}
  65.      *
  66.      * @access protected
  67.      * @param string Name of parameter
  68.      * @param string DB field type ('string', 'number', 'date' )
  69.      * @param mixed Pointer to value of parameter
  70.      */
  71.     function dbchange$dbfieldname$dbfieldtype$valuepointer )
  72.     {
  73.         $this->dbchanges[$dbfieldname]['type'$dbfieldtype;
  74.         $this->dbchanges[$dbfieldname]['value'$valuepointer ;
  75.     }
  76.  
  77.  
  78.     /*
  79.      * DataObject::dbupdate(-)
  80.      *
  81.      * Update the DB based on previously recorded changes
  82.      */
  83.     function dbupdate)
  84.     {
  85.         global $DB;
  86.  
  87.         if$this->ID == die'New object cannot be updated!' );
  88.  
  89.         ifcount$this->dbchanges == )
  90.             return;    // No changes!
  91.  
  92.         $sql_changes array();
  93.         foreach$this->dbchanges as $loop_dbfieldname => $loop_dbchange )
  94.         {
  95.             // Get changed value:
  96.             eval('$loop_value = $this->'$loop_dbchange['value'].';');
  97.             // Prepare matching statement:
  98.             switch$loop_dbchange['type')
  99.             {
  100.                 case 'string':
  101.                     $sql_changes[$loop_dbfieldname" = '"$DB->escape$loop_value )"' ";
  102.                     break;
  103.  
  104.                 default:
  105.                     $sql_changes[$loop_dbfieldname" = $loop_value ";
  106.             }
  107.         }
  108.  
  109.         // Prepare full statement:
  110.         $sql "UPDATE $this->dbtablename SET "implode', '$sql_changes )" WHERE $this->dbIDname = $this->ID";
  111.         //echo $sql;
  112.  
  113.         $DB->query($sql);
  114.  
  115.         // Reset changes in object:
  116.         $this->dbchanges array();
  117.     }
  118.  
  119.  
  120.     /*
  121.      * DataObject::dbinsert(-)
  122.      *
  123.      * Insert object into DB based on previously recorded changes
  124.      */
  125.     function dbinsert)
  126.     {
  127.         global $DB;
  128.  
  129.         if$this->ID != die'Existing object cannot be inserted!' );
  130.  
  131.         $sql_fields array();
  132.         $sql_values array();
  133.         foreach$this->dbchanges as $loop_dbfieldname => $loop_dbchange )
  134.         {
  135.             // Get changed value:
  136.             eval('$loop_value = $this->'$loop_dbchange['value'].';');
  137.             // Prepare matching statement:
  138.             $sql_fields[$loop_dbfieldname;
  139.             switch$loop_dbchange['type')
  140.             {
  141.                 case 'string':
  142.                     $sql_values[$DB->quote$loop_value );
  143.                     break;
  144.  
  145.                 default:
  146.                     $sql_values[$DB->null$loop_value );
  147.             }
  148.         }
  149.  
  150.         // Prepare full statement:
  151.         $sql "INSERT INTO {$this->dbtablename} ("implode( ', ', $sql_fields )") VALUES ("implode( ', ', $sql_values )")";
  152.         // echo $sql;
  153.         $DB->query($sql);
  154.  
  155.         // store ID for newly created db record
  156.         $this->ID = $DB->insert_id;
  157.  
  158.         // Reset changes in object:
  159.         $this->dbchanges array();
  160.     }
  161.  
  162.  
  163.     /*
  164.      * DataObject::dbdelete(-)
  165.      *
  166.      * Delete object from DB
  167.      */
  168.     function dbdelete( )
  169.     {
  170.         global $DB;
  171.  
  172.         if( $this->ID == die'Non persistant object cannot be deleted!' );
  173.  
  174.         $query "DELETE FROM $this->dbtablename
  175.                             WHERE $this->dbIDname = $this->ID";
  176.         $DB->query($query);
  177.     }
  178.  
  179.  
  180.     /**
  181.      * Get a member param by its name
  182.      *
  183.      * {@internal DataObject::get(-) }}
  184.      *
  185.      * @param mixed Name of parameter
  186.      * @return mixed Value of parameter
  187.      */
  188.     function get( $parname )
  189.     {
  190.         return $this->$parname;
  191.     }
  192.  
  193.  
  194.     /**
  195.      * Get a ready-to-display member param by its name
  196.      *
  197.      * Same as disp but don't echo
  198.      *
  199.      * {@internal DataObject::dget(-) }}
  200.      *
  201.      * @param string Name of parameter
  202.      * @param string Output format, see {@link format_to_output()}
  203.      */
  204.     function dget( $parname, $format = 'htmlbody' )
  205.     {
  206.         // Note: we call get again because of derived objects specific handlers !
  207.         return format_to_output( $this->get($parname)$format );
  208.     }
  209.  
  210.  
  211.     /**
  212.      * Display a member param by its name
  213.      *
  214.      * {@internal DataObject::disp(-) }}
  215.      *
  216.      * @param string Name of parameter
  217.      * @param string Output format, see {@link format_to_output()}
  218.      */
  219.     function disp( $parname, $format = 'htmlbody' )
  220.     {
  221.         // Note: we call get again because of derived objects specific handlers !
  222.         echo format_to_output( $this->get($parname)$format );
  223.     }
  224.  
  225.     /**
  226.      * Set param value
  227.      *
  228.      * {@internal DataObject::set_param(-) }}
  229.      *
  230.      * @param string Name of parameter
  231.      * @param string DB field type ('string', 'number', 'date' )
  232.      * @param mixed Value of parameter
  233.      */
  234.     function set_param( $parname, $fieldtype, $parvalue )
  235.     {
  236.         // Set value:
  237.         $this->$parname $parvalue;
  238.         // Remember change for later db update:
  239.         $this->dbchange$this->dbprefix$parname $fieldtype$parname );
  240.     }
  241.  
  242.     /**
  243.      * Template function: Displays object ID
  244.      *
  245.      * {@internal DataObject::ID(-) }}
  246.      */
  247.     function ID()
  248.     {
  249.         echo $this->ID;
  250.     }
  251. }

Documentation generated on Tue, 20 May 2008 01:52:47 +0200 by phpDocumentor 1.4.2