<?php
/**
* dataObject
*
* Base definition of the data Object
*
* @package
* @author Ivan CampaƱa <hide@address.com>
* @copyright Copyright (c) 2005 - GNU/GPL License
* @version 0.1
* @access public
**/
class dataObject {
// To make it easier to recognize all the private variables and functions start with an underscore (_)
var $_conn = null;
var $_tbl = '';
var $_tbl_key = null;
var $_error = '';
var $_fields = array( );
var $_loaded = false;
// Private methods
/**
* dataObject::_getColumns()
*
* Get the columns definition for the current table
* including the primary keys
*
* @return
**/
function _getColumns( )
{
$columns = $this->_conn->MetaColumns( $this->_tbl);
foreach( $columns as $column) {
$cIndex = $column->name;
switch ( $column->type) {
case 'float':
case 'double':
case 'integer':
$value = 0;
break;
default:
$value = null;
break;
}
$this->$cIndex = $value;
$this->_fields[$cIndex] = $value;
}
$pKeys = $this->_conn->MetaPrimaryKeys( $this->_tbl);
if ( sizeof( $pKeys) == 1) {
$this->_tbl_key = $pKeys[0];
} else {
$this->_tbl_key = Array( ); // Must be an array to support multiple fields key
foreach ( $pKeys as $pk) {
$this->_tbl_key[] = $pk;
}
}
}
/**
* dataObject::_hasKey()
*
* checks if the primary key(s) contains at least some value
* supports single or multiple field PK
*
* @return boolean
**/
function _hasKey( )
{
if ( is_array( $this->_tbl_key)) {
// Table with multiple key values
reset( $this->_tbl_key);
$keyCounter = 0;
foreach( $this->_tbl_key as $key) {
if ( strlen( $this->_tbl_key) > 0) {
$keyCounter++;
}
}
if ( $keyCounter == sizeof( $this->_tbl_key)) return true;
return false;
} else {
// Treats the key is a string to make sure is has something as content
if ( strlen( $this->_fields[$this->_tbl_key]) > 0) {
return true;
} else{
return false;
}
}
}
/**
* dataObject::_check()
*
* @return
**/
function _check( )
{
// Should make sure all the primary keys have been entered
}
/**
* dataObject::_getWhere()
*
* Returns the where sentence needed to get the data from the DB
* can be also used to make the update sentece
*
* @return
**/
function _getWhere( )
{
if ( !is_array( $this->_tbl_key)) {
$value = $this->_fields[$this->_tbl_key];
$where_string = "{$this->_tbl_key}='{$value}'";
} else {
reset( $this->_tbl_key);
$where_fields = array( );
foreach ( $this->_tbl_key as $value) {
$where_fields[] = "{$value} = {$this->_fields[$value]}";
}
$where_string = implode( " AND ", $where_fields);
}
return $where_string;
}
// Public Methods
// Constructor
function dataObject( &$connection, $table)
{
if ( $connection->IsConnected( )) {
$this->_conn = $connection;
$this->_tbl = $table;
$this->_conn->debug = DEBUG;
$this->_getColumns( );
} else {
$this->_error = "Not connected to the Database";
}
}
/*
Functions modified from
Code taken from Mambo Open Source 4.5
http://mamboforge.net/
*/
/**
* dataObject::store()
*
* Saves the object info into the database, checks if the data
* exists to update, else generates the insert code.
*
* @return boolean
**/
function store( )
{
if ( $this->_hasKey( ) and $this->_loaded) {
// original parameters: &$rs, $arrFields, $forceUpdate=false,$magicq=false, $force=null
$sqlString = $this->_conn->getUpdateSQL( $this->_rs, $this->_fields);
} else {
$sqlString = $this->_conn->getInsertSQL( $this->_tbl, $this->_fields);
}
if ( $sqlString != "") {
if ( $this->_conn->execute( $sqlString) === false) {
$this->_error = get_class( $this) . "::store failed <br />" . $this->_conn->ErrorMsg( );
return false;
} else {
return true;
}
}
}
/**
* dataObject::load()
*
* Loads a register from the DB, receives a parameter that
* can be either a single key or a multiple field key, using
* an array.
*
* @param variant $id
* @return
**/
function load( $id = null)
{
if ( $id == null) return false;
if ( is_array( $id)) {
if ( sizeof( $id) != sizeof( $this->_tbl_key)) {
$this->_error = "Not enough parameters to load a register";
return false;
}
foreach( $id as $key => $value) {
unset( $k);
$k = $this->_tbl_key[$key];
// $this->$k = $value;
$this->_fields[$key] = $value;
}
} else {
$k = $this->_tbl_key;
// $this->$k = $id;
$this->_fields[$k] = $id;
}
// debug_var($this->_tbl_key);exit();
$rs = $this->_conn->execute( "SELECT * FROM {$this->_tbl} WHERE " . $this->_getWhere( ));
if ( $rs->RowCount( ) > 0) {
if ( $this->_hasKey( )) {
$this->bindData( $rs);
$this->_loaded = true;
return true;
}
} else {
return false;
}
}
/**
* dataObject::bindData()
*
* Associates the data from a recordset with the current object
*
* @param $recordset
* @return
**/
function bindData( $recordset)
{
/*
foreach (get_object_vars($this) as $k => $v) {
if (isset($recordset->fields[$k])) {
$this->$k = (get_magic_quotes_gpc()) ? stripslashes( $recordset->fields[$k] ) : $recordset->fields[$k];
}
}*/
$this->_rs = $recordset;
$this->_fields = $recordset->fields;
}
/**
* dataObject::get()
*
* Returns the value of one of the object properties
*
* @param $_property
* @return
**/
function get( $_property)
{
if ( isset( $this->_fields[$_property])) {
return $this->_fields[$_property];
} else {
return null;
}
}
/**
* dataObject::set()
*
* Establishes the value of one of the object properties
*
* @param $_property
* @param $_value
* @return
**/
function set( $_property, $_value)
{
// $this->$_property = $_value;
$this->_fields[$_property] = $_value;
}
/**
* dataObject::delete()
*
* Deletes the current register from the DB
*
* @return
**/
function delete( )
{
// if (!$this->canDelete( $msg )) {
// return $msg;
// }
$result = $this->_conn->execute( "DELETE FROM {$this->_tbl} WHERE " . $this->_getWhere( ));
if ( $result === false) {
return false;
} else {
return true;
}
}
}
?>