Source for file _dataobjectcache.class.php
Documentation is available at _dataobjectcache.class.php
* This file implements the DataObjectCache class.
* This file is part of the evoCore framework - {@link http://evocore.net/}
* See also {@link http://sourceforge.net/projects/evocms/}.
* @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}
* Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
* Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.
* {@internal License choice
* - If you have received this file as part of a package, please find the license.txt file in
* the same folder or the closest folder above for complete license terms.
* - If you have received this file individually (e-g: from http://cvs.sourceforge.net/viewcvs.py/evocms/)
* then you must choose one of the following licenses before using the file:
* - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
* - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
* {@internal Open Source relicensing agreement:
* Daniel HAHLER grants Francois PLANQUE the right to license
* Daniel HAHLER's contributions to this file and the b2evolution project
* under any OSI approved OSS license (http://www.opensource.org/licenses/).
* PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license
* PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project
* under any OSI approved OSS license (http://www.opensource.org/licenses/).
* {@internal Below is a list of authors who have contributed to design/coding of this file: }}
* @author blueyed: Daniel HAHLER.
* @author fplanque: Francois PLANQUE
* @version $Id: _dataobjectcache.class.php,v 1.6.2.2 2006/11/04 19:54:53 fplanque Exp $
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
* Data Object Cache Class
* Class name of objects in this cache:
* Copy of previous object array
* @see DataObjectCache::clear()
* @param string Name of DataObject class we are cacheing
* @param boolean true if it's OK to just load all items!
* @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 DataObjectCache( $objtype, $load_all, $tablename, $prefix =
'', $dbIDname, $name_field =
NULL, $order_by =
'' )
$this->load_all =
$load_all;
if( empty( $name_field ) )
// Instantiate a custom object
$obj =
new $objtype( $row ); // COPY !!
* Load the cache **extensively**
$Debuglog->add( get_class($this).
' - Loading <strong>'.
$this->objtype.
'(ALL)</strong> into cache', 'dataobjects' );
foreach( $DB->get_results( $sql ) as $row )
// Instantiate a custom object
* Load a list of objects into the cache
* @param string list of IDs of objects to load
$Debuglog->add( "Loading <strong>$this->objtype($req_list)</strong> into cache", 'dataobjects' );
foreach( $DB->get_results( $sql ) as $row )
$this->add( new $objtype( $row ) );
// TODO: use instantiate()
* Get an array of all (loaded) IDs.
foreach( $this->cache as $obj )
* Add a dataobject to the cache
$Debuglog->add( 'No object to add!', 'dataobjects' );
if( isset
($this->cache[$Obj->ID]) )
// fplanque: I don't want an extra (and expensive) comparison here. $this->cache[$Obj->ID] === $Obj. If you need this you're probably misusing the cache.
$Debuglog->add( $this->objtype.
': Object with ID '.
$Obj->ID.
' is already cached', 'dataobjects' );
// If the object is valid and not already cached:
$this->cache[$Obj->ID] =
& $Obj;
* Instantiate a DataObject from a table row and then cache it.
* @param Object Database row
// Get ID of the object we'ere preparing to instantiate...
if( isset
( $this->cache[$obj_ID] ) )
{ // Already in cache, do nothing!
{ // Already in shadow, recycle object:
{ // Not already cached, add new object:
return $this->cache[$obj_ID];
* Clear the cache **extensively**
function clear( $keep_shadow =
false )
{ // Keep copy of cache in case we try to re instantiate previous object:
* Get an object from cache by ID
* Load the cache if necessary (all at once if allowed).
* @param integer ID of object to load
* @param boolean true if function should die on error
* @param boolean true if function should die on empty/null
* @return reference on cached object
function & get_by_ID( $req_ID, $halt_on_error =
true, $halt_on_empty =
true )
if( !empty( $this->cache[ $req_ID ] ) )
// $Debuglog->add( "Accessing $this->objtype($req_ID) from cache", 'dataobjects' );
return $this->cache[ $req_ID ];
{ // Not in cache, but not everything is loaded yet
{ // It's ok to just load everything:
{ // Load just the requested object:
$Debuglog->add( "Loading <strong>$this->objtype($req_ID)</strong> into cache", 'dataobjects' );
// Note: $req_ID MUST be an unsigned integer. This is how DataObject works.
if( $row =
$DB->get_row( $sql, OBJECT, 0, 'DataObjectCache::get_by_ID()' ) )
$Debuglog->add( 'Could not add() object to cache!', 'dataobjects' );
$Debuglog->add( 'Could not get DataObject by ID. Query: '.
$sql, 'dataobjects' );
if( empty( $this->cache[ $req_ID ] ) )
{ // Requested object does not exist
// $Debuglog->add( 'failure', 'dataobjects' );
return $this->cache[ $req_ID ];
* Get an object from cache by name
* Load the cache if necessary (all at once if allowed).
* @param integer ID of object to load
* @param boolean true if function should die on error
* @param boolean true if function should die on empty/null
* @return reference on cached object
function & get_by_name( $req_name, $halt_on_error =
true, $halt_on_empty =
true )
debug_die( 'DataObjectCache::get_by_name() : No name field to query on' );
// Load just the requested object:
$Debuglog->add( "Loading <strong>$this->objtype($req_name)</strong>", 'dataobjects' );
WHERE $this->name_field = ".
$DB->quote($req_name);
if( $db_row =
$DB->get_row( $sql, OBJECT, 0, 'DataObjectCache::get_by_name()' ) )
$resolved_ID =
$db_row->{$this->dbIDname};
$Debuglog->add( 'success; ID = '.
$resolved_ID, 'dataobjects' );
if( ! isset
( $this->cache[$resolved_ID] ) )
{ // Object is not already in cache:
$Debuglog->add( 'Adding to cache...', 'dataobjects' );
//$Obj = new $this->objtype( $row ); // COPY !!
//if( ! $this->add( $this->new_obj( $db_row ) ) )
$Debuglog->add( 'Could not add() object to cache!', 'dataobjects' );
return $this->cache[$resolved_ID];
$Debuglog->add( 'Could not get DataObject by name.', 'dataobjects' );
* Remove an object from cache by ID
* @param integer ID of object to remove
unset
( $this->cache[$req_ID] );
* Delete an object from DB by ID.
* @param integer ID of object to delete
if( isset
( $this->cache[$req_ID] ) )
$this->cache[$req_ID]->dbdelete();
* Display form option list with cache contents
* Load the cache if necessary
* @todo Shouldn't this use {@link option_list_return()}?
* @param integer selected ID
* @param boolean provide a choice for "none" with ID ''
function option_list( $default =
0, $allow_none =
false, $method =
'name' )
{ // We have not loaded all items so far, but we're allowed to... so let's go:
if( empty($default) ) echo
' selected="selected"';
echo
'>', T_('None') ,'</option>'.
"\n";
foreach( $this->cache as $loop_Obj )
echo
'<option value="'.
$loop_Obj->ID.
'"';
if( $loop_Obj->ID ==
$default ) echo
' selected="selected"';
* Returns form option list with cache contents
* Load the cache if necessary
* @param integer selected ID
* @param boolean provide a choice for "none" with ID ''
{ // We have not loaded all items so far, but we're allowed to... so let's go:
$r .=
'<option value=""';
if( empty($default) ) $r .=
' selected="selected"';
$r .=
'>'.
T_('None').
'</option>'.
"\n";
foreach( $this->cache as $loop_Obj )
$r .=
'<option value="'.
$loop_Obj->ID.
'"';
if( $loop_Obj->ID ==
$default ) $r .=
' selected="selected"';
$r .=
$loop_Obj->$method();
* $Log: _dataobjectcache.class.php,v $
* Revision 1.6.2.2 2006/11/04 19:54:53 fplanque
* Reinjected old Log blocks. Removing them from CVS was a bad idea -- especially since Daniel has decided branch 1.9 was his HEAD...
* Revision 1.6 2006/08/02 16:34:16 yabs
* corrected $row to $db_row in function get_by_name()
* Revision 1.5 2006/06/14 17:26:13 fplanque
* Revision 1.4 2006/04/19 20:13:50 fplanque
* do not restrict to :// (does not catch subdomains, not even www.)
* Revision 1.3 2006/04/14 19:25:32 fplanque
* evocore merge with work app
* Revision 1.2 2006/03/12 23:08:58 fplanque
* Revision 1.1 2006/02/23 21:11:57 fplanque
* File reorganization to MVC (Model View Controller) architecture.
* See index.hml files in folders.
* (Sorry for all the remaining bugs induced by the reorg... :/)
* Revision 1.34 2006/02/08 12:24:37 blueyed
* Revision 1.33 2005/12/30 20:13:39 fplanque
* UI changes mostly (need to double check sync)
* Revision 1.32 2005/12/12 19:21:21 fplanque
* big merge; lots of small mods; hope I didn't make to many mistakes :]
* Revision 1.28 2005/11/16 21:53:49 fplanque
* Revision 1.27 2005/11/16 12:21:15 blueyed
* Revision 1.26 2005/11/09 03:20:05 blueyed
* Revision 1.25 2005/10/03 22:50:53 blueyed
* Fixed E_NOTICE for PHP 4.4.0 and probably 5.1.x (again). Functions that return by reference must not return values!
* Revision 1.24 2005/09/29 15:26:15 fplanque
* Revision 1.23 2005/09/18 01:46:55 blueyed
* Fixed E_NOTICE for return by reference (PHP 4.4.0)
* Revision 1.22 2005/09/06 17:13:54 fplanque
* stop processing early if referer spam has been detected
* Revision 1.21 2005/08/02 18:15:14 fplanque
* fix for correct NULL handling
* Revision 1.20 2005/07/15 18:10:07 fplanque
* allow instantiating of member objects (used for preloads)
* Revision 1.19 2005/06/10 18:25:44 fplanque
* Revision 1.18 2005/05/16 15:17:13 fplanque
* Revision 1.17 2005/05/11 13:21:38 fplanque
* allow disabling of mediua dir for specific blogs
* Revision 1.16 2005/04/19 16:23:02 fplanque
* improved meta data handling
* Revision 1.15 2005/03/14 20:22:19 fplanque
* refactoring, some cacheing optimization
* Revision 1.14 2005/03/02 15:24:29 fplanque
* allow get_by_ID(NULL) in some situations
* Revision 1.13 2005/02/28 09:06:32 blueyed
* removed constants for DB config (allows to override it from _config_TEST.php), introduced EVO_CONFIG_LOADED
* Revision 1.12 2005/02/14 21:17:45 blueyed
* optimized cache handling
* Revision 1.11 2005/02/09 00:27:13 blueyed
* Removed deprecated globals / userdata handling
* Revision 1.10 2005/02/08 04:45:02 blueyed
* improved $DB get_results() handling
* Revision 1.9 2005/01/20 18:46:26 fplanque
* Revision 1.8 2005/01/13 19:53:50 fplanque
* Refactoring... mostly by Fabrice... not fully checked :/
* Revision 1.7 2004/12/27 18:37:58 fplanque
* changed class inheritence
* Revision 1.5 2004/12/21 21:18:38 fplanque
* Finished handling of assigning posts/items to users
* Revision 1.4 2004/12/17 20:38:52 fplanque
* started extending item/post capabilities (extra status, type)
* Revision 1.3 2004/10/14 18:31:25 blueyed
* Revision 1.2 2004/10/14 16:28:40 fplanque
* Revision 1.1 2004/10/13 22:46:32 fplanque
* Revision 1.18 2004/10/12 10:27:18 fplanque
* Edited code documentation.