Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/domain/proxy/entity.php
<?php
/**
 * @version		1.0.0
 * @category	Anahita Social Engine™
 * @copyright	Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
 * @license		GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
 * @link     	http://www.anahitapolis.com
 */

class AnDomainProxyEntity extends KPatternDecorator 
{	
	protected static $_proxies = array();
	
	protected static $_queries = array();
	
	protected $_method;
		
	protected $_model;
		
	protected $_primary_key;
		
	protected $_query;
	
	protected $_parent_key;
	
	protected $_parent_key_value;
	
	public function __construct($options=array())
	{				
		$this->_model 	    = (string) $options['model'];
		$this->_parent_key  = $options['parent_key'];
		$this->_parent_key_value = $options['parent_key_value'];

		if ( $this->_parent_key_value ) {
			
			if ( !isset(self::$_proxies[$this->_model]) ) 
				self::$_proxies[$this->_model] = array();
			
			if ( !isset(self::$_proxies[$this->_model][$this->_parent_key]) )
				self::$_proxies[$this->_model][$this->_parent_key] = array(); 
			
			self::$_proxies[$this->_model][$this->_parent_key][] = $this->_parent_key_value;
		}
	}
	
	public function __get($property)
	{
		if ( $property == $this->_parent_key && !isset($this->_object) ) {
			return $this->_parent_key_value;
		}
		return parent::__get($property);
	}
	
	/**
	 * gets the model - it will try to load all the model of the same type
	 * @return 
	 * @param $options Object[optional]
	 */
	public function getObject($options=array())
	{		
	
		if ( !$this->_parent_key_value ) 
			return null;
	
		if ( !isset($this->_object) ) {
			
			$mapper        = KFactory::get('lib.anahita.domain.factory.mapper')->get($this->_model);
			$context       = KFactory::get('lib.anahita.domain.context');
			$method 	   = $this->_method;
			$query     	   = $mapper->getQuery()->where($this->_parent_key,'=',$this->_parent_key_value);
			
			if  ( !$mapper->getDomainContext()->exists($query) ) {
				
				//if the entity is not in context, look for all the similar proxies 
				//and load all of them at once
				$uniqukeys  = array_unique(self::$_proxies[$this->_model][$this->_parent_key]);
				$_query     = $mapper->getQuery()->where($this->_parent_key, 'IN', $uniqukeys);
				
				if ( !isset(self::$_queries[(string)$_query]) ) {
					$_query->fetchAll();
					self::$_queries[(string)$_query] = 1;
				}
			}
			
			$this->_object = $query->fetch();
		} 
		
		return $this->_object;
	}
}
Return current item: Anahita Social Engine