Source for file DatabaseEntity.class.php
Documentation is available at DatabaseEntity.class.php
* @package data_structures
require_once CLASSPATH. "data_structures/ABSTObject.class.php";
* Class to generate objects from database tables. It makes most sense to use
* this class as an abstract type.
* class Article extends DatabaseEntity
* function Article( $int_id = null )
* parent::DatabaseEntity( $GLOBALS["dbt_article"] );
* if ( $int_id != null && is_numeric( $int_id ) )
* { parent::initiateObjectFromTuple("id_article = '{$int_id}'"); }
* } // end of class Article
* @package data_structures
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (C) 2006 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Stores the reference to an object of a database entity.
* @var DatabaseEntity $____parent_obj
* Stores the reference to an object of a database table.
* @var ABSTDatabaseTable $____dbt
* Stores the where-clause to generate the data of this entity.
* @var array $____str_where
* Stores the key fieldnames.
* @var array $____key_arr
* Stores the references to the objects of the key database tables.
* @var array $____key_table_arr
* Stores the references to the objects of the key entities.
* @var array $____key_object_arr
* Stores the references to other entities.
* @var array $____entity_arr
* Stores the references to other entities. But with a relation name as key
* in the first array dimension.
* @var array $____relation_arr
* Put in a reference to an object which is a subtype of the type
* ABSTDatabaseTable or a reference to an object which is a subtype of the
* @param ABSTDatabaseTable $dbt_obj
* Initiates this object by using the data of a tuple from a database result.
* The database table is managed by an object of the GilliGan-Framework. See
* in package databases for further information. The database tables object
* will be stored in the Field $____dbt of this object. The original
* fieldnames from the database table will be used for the fieldnames of this
* Beware, only the first found tuple will used for the initiation,
* therefore you better should use the primary key to grab the data from
* the underlying database table.
* @param string $str_where
$res_arr = $this->____dbt->getSingleDataset( $str_where );
foreach ( $res_arr as $fieldname => $value )
if ( !is_int( $fieldname ) && !empty( $fieldname ) )
{ $this->$fieldname = $value; }
* Adds a keytable to this object.
* @author Daniel Plücken <daniel@debakel.net>
* @param string $key Fieldname that refers to the key table.
* @param ABSTDatabaseTable $dbt_obj Reference to the key table.
$pkey_arr = $dbt_obj->getPrimaryKey();
$tmp_obj->initiateObjectFromTuple( $pkey_arr[0]. " = '". $this->$key. "'" );
* @param DatabaseEntity $dbe
* @param string $named_relation For example is_in_category ...
function addEntity( &$dbe, $named_relation = "" )
$dbe->____parent_obj = & $this;
if ( empty( $named_relation ) )
* Describes an "n to m"-Relation to this object.
* @param ABSTDatabaseTable $dbt Reference to the key table.
* @param string $str_key Fieldname that refers to the key
* @param string $str_additional_where
* @param string $named_relation For example is_in_category ...
&$dbt, $str_key, $str_additional_where = "",
$pkey_arr = $this->____dbt->getPrimaryKey();
$str_where_entity = $str_key. " = '". $this->$pkey_arr[0]. "'";
if ( !empty( $str_additional_where ) )
{ $str_where_entity .= " AND ". $str_additional_where; }
$res_arr = $dbt->getDatasets(
for ( $i = 0; $i < count( $res_arr ); $i++ )
foreach ( $res_arr[$i] as $fieldname => $value )
{ $tmp_obj->$fieldname = $value; }
$this->addEntity( $tmp_obj, $named_relation );
* Returns an array of this object. The objects are generated from a hurd of
* tuples collected from a result of a database query.
* @author Daniel Plücken <daniel@debakel.net>
* @author Daniel Dornhardt <daniel@dornhardt.com>
* @param ABSTDatabaseTable $dbt_obj Reference to the table from which
* @param string $str_where Where-clause to fetch the tuple.
* @param string $classname if used for subclasses of
* DatabaseEntity, class $classname is
* used for the construction objects so
* members / methods of subclass can be
* @param boolean $primary_key_as_array_keys If this is set to true, the
* keys of the output array will
* be the same like the primary
* key value for the matching
* tuple. THIS DOESN'T MAKE SENSE
$primary_key_as_array_keys = false
is_a( $dbt_obj, "ABSTView" )
for ( $i = 0; $i < count( $res_arr ); $i++ )
$tmp_arr[] = & new $classname( $dbt_obj );
foreach ( $res_arr[$i] as $fieldname => $value )
if ( empty( $tmp_arr[count($tmp_arr)- 1]->$fieldname ) )
{ $tmp_arr[count($tmp_arr)- 1]->$fieldname = $value; }
!$primary_key_as_array_keys
|| !($pk_arr = $dbt_obj->getPrimaryKey())
for ( $i = 0; $i < count( $res_arr ); $i++ )
$tmp_arr[] = & new $classname( $dbt_obj );
foreach ( $res_arr[$i] as $fieldname => $value )
{ $tmp_arr[count($tmp_arr)- 1]->$fieldname = $value; }
for ( $i = 0; $i < count( $res_arr ); $i++ )
foreach ( $pk_arr as $key_name )
{ $str_pk_impl .= $res_arr[$i][$key_name]; }
$tmp_arr[$str_pk_impl] = & new $classname( $dbt_obj );
foreach ( $res_arr[$i] as $fieldname => $value )
{ $tmp_arr[$str_pk_impl]->$fieldname = $value; }
* Trys to delete a dataset in the database table with values from the
$p_arr = $this->____dbt->getPrimaryKey();
$str_where = $str_name. " = '{$this->$str_name}' ";
foreach ( $p_arr as $str_name )
{ $str_where .= " AND {$str_name} = '{$this->$str_name}' "; }
return $this->____dbt->delete( $str_where );
* Replaces this object in the database table.
* @author Daniel Dornhardt <daniel@dornhardt.com>
* @author Daniel Plücken <daniel@debakel.net> (auto_increment-handle)
$keys = $this->____dbt->getFieldnames();
foreach ( $keys as $fieldname )
{ $values[] = $this->$fieldname; }
$auto_increment_field = $this->____dbt->getAutoIncrementField();
// It is an update or there is no auto increment field!
empty( $auto_increment_field )
|| !empty( $this->$auto_increment_field )
{ return $this->____dbt->replace( $keys, $values ); }
// It is an insert and an auto increment field exists!
$bool_ret = $this->____dbt->replace( $keys, $values );
{ $this->$auto_increment_field = $GLOBALS["insert_id"]; }
* Puts the values of the passed kind of request variables into the fields
* of this object. This is useful to update a dataset with entered form data
* before writing it back to the database table were it come from. Use the
* method "replace" to to so.
* @param array $request_type_arr
$request_type_arr = array( "GET", "POST", "REQUEST" )
{ $request_type_arr = array( $request_type_arr ); }
$field_arr = $this->____dbt->getRealFieldnames();
if ( in_array( "GET", $request_type_arr ) )
foreach ( $field_arr as $field_name )
if ( isset ( $_GET[ $field_name ] ) )
{ $this->$field_name = stripslashes( $_GET[ $field_name ] ); }
if ( in_array( "POST", $request_type_arr ) )
foreach ( $field_arr as $field_name )
if ( isset ( $_POST[ $field_name ] ) )
{ $this->$field_name = stripslashes( $_POST[ $field_name ] ); }
if ( in_array( "REQUEST", $request_type_arr ) )
foreach ( $field_arr as $field_name )
if ( isset ( $_REQUEST[ $field_name ] ) )
{ $this->$field_name = stripslashes( $_REQUEST[ $field_name ] ); }
if ( in_array( "GET", $request_type_arr ) )
foreach ( $field_arr as $field_name )
if ( isset ( $_GET[ $field_name ] ) )
{ $this->$field_name = $_GET[ $field_name ]; }
if ( in_array( "POST", $request_type_arr ) )
foreach ( $field_arr as $field_name )
if ( isset ( $_POST[ $field_name ] ) )
{ $this->$field_name = $_POST[ $field_name ]; }
if ( in_array( "REQUEST", $request_type_arr ) )
foreach ( $field_arr as $field_name )
if ( isset ( $_REQUEST[ $field_name ] ) )
{ $this->$field_name = $_REQUEST[ $field_name ]; }
* Puts the fields of this Object, that correspond with the fields of
* database table, into the $_POST-Array.
$tmp_arr = $this->____dbt->getFieldnames();
foreach ( $tmp_arr as $value )
$_POST[$value] = $this->$value;
$_REQUEST[$value] = $this->$value;
* Puts the fields of this Object, that correspond with the fields of
* database table, into the $_GET-Array.
$tmp_arr = $this->____dbt->getFieldnames();
foreach ( $tmp_arr as $value )
$_GET[$value] = $this->$value;
$_REQUEST[$value] = $this->$value;
* Prints out the Structure of this Object without the management data.
* @author Daniel Plücken <daniel.pluecken@wfp2.com>
} // End of class DatabaseEntity
|