Source for file _class_dataobject.php
Documentation is available at _class_dataobject.php
* "data objects by fplanque" :P
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
* @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );
* This is typically an abstract class, useful only when derived.
* Unique ID of object in database
* Please use get/set functions to read or write this param
var $ID =
0; // This will be the ID in the DB
var $dbchanges =
array();
* {@internal DataObject::DataObject(-) }}
* @param string Name of table in database
* @param string Prefix of fields in the table
* @param string Name of the ID field (including prefix)
function DataObject( $tablename, $prefix =
'', $dbIDname =
'ID' )
$this->dbtablename =
$tablename;
$this->dbprefix =
$prefix;
$this->dbIDname =
$dbIDname;
* Records a change that will need to be updated in the db
* {@internal DataObject::dbchange(-) }}
* @param string Name of parameter
* @param string DB field type ('string', 'number', 'date' )
* @param mixed Pointer to value of parameter
function dbchange( $dbfieldname, $dbfieldtype, $valuepointer )
$this->dbchanges[$dbfieldname]['type'] =
$dbfieldtype;
$this->dbchanges[$dbfieldname]['value'] =
$valuepointer ;
* DataObject::dbupdate(-)
* Update the DB based on previously recorded changes
if( $this->ID ==
0 ) die( 'New object cannot be updated!' );
if( count( $this->dbchanges ) ==
0 )
foreach( $this->dbchanges as $loop_dbfieldname =>
$loop_dbchange )
eval
('$loop_value = $this->'.
$loop_dbchange['value'].
';');
// Prepare matching statement:
switch( $loop_dbchange['type'] )
$sql_changes[] =
$loop_dbfieldname.
" = '".
$DB->escape( $loop_value ).
"' ";
$sql_changes[] =
$loop_dbfieldname.
" = $loop_value ";
// Prepare full statement:
$sql =
"UPDATE $this->dbtablename SET ".
implode( ', ', $sql_changes ).
" WHERE $this->dbIDname = $this->ID";
// Reset changes in object:
$this->dbchanges =
array();
* DataObject::dbinsert(-)
* Insert object into DB based on previously recorded changes
if( $this->ID !=
0 ) die( 'Existing object cannot be inserted!' );
foreach( $this->dbchanges as $loop_dbfieldname =>
$loop_dbchange )
eval
('$loop_value = $this->'.
$loop_dbchange['value'].
';');
// Prepare matching statement:
$sql_fields[] =
$loop_dbfieldname;
switch( $loop_dbchange['type'] )
$sql_values[] =
$DB->quote( $loop_value );
$sql_values[] =
$DB->null( $loop_value );
// Prepare full statement:
$sql =
"INSERT INTO {$this->dbtablename} (
". implode( ', ', $sql_fields ). ") VALUES (". implode( ', ', $sql_values ). ")";
// store ID for newly created db record
$this->ID =
$DB->insert_id;
// Reset changes in object:
$this->dbchanges =
array();
* DataObject::dbdelete(-)
if( $this->ID ==
0 ) die( 'Non persistant object cannot be deleted!' );
$query =
"DELETE FROM $this->dbtablename
WHERE
$this->dbIDname =
$this->ID";
* Get a member param by its name
* {@internal DataObject::get(-) }}
* @param mixed Name of parameter
* @return mixed Value of parameter
* Get a ready-to-display member param by its name
* Same as disp but don't echo
* {@internal DataObject::dget(-) }}
* @param string Name of parameter
* @param string Output format, see {@link format_to_output()}
function dget( $parname, $format = 'htmlbody' )
// Note: we call get again because of derived objects specific handlers !
return format_to_output( $this->get($parname), $format );
* Display a member param by its name
* {@internal DataObject::disp(-) }}
* @param string Name of parameter
* @param string Output format, see {@link format_to_output()}
function disp( $parname, $format = 'htmlbody' )
// Note: we call get again because of derived objects specific handlers !
echo format_to_output( $this->get($parname), $format );
* {@internal DataObject::set_param(-) }}
* @param string Name of parameter
* @param string DB field type ('string', 'number', 'date' )
* @param mixed Value of parameter
function set_param( $parname, $fieldtype, $parvalue )
$this->$parname =
$parvalue;
// Remember change for later db update:
$this->dbchange( $this->dbprefix.
$parname , $fieldtype, $parname );
* Template function: Displays object ID
* {@internal DataObject::ID(-) }}