Location: PHPKode > projects > Anahita Social Engine > dependencies/plg_system_socialengine/socialengine/domain/description/relationship/manytoone.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 AnDomainDescriptionRelationshipManytoone extends AnDomainDescriptionRelationshipAbstract 
{	
	protected $_type_field = null;
	
	protected $_inverse	   = null;
	
	protected $_base_model = null;
	
	public function __construct($options)
	{	
		$default = array(			
			'type_field'		=> $options['name'].'_type',
			'child_key'		    => $options['name'].'_id'  ,
			'inverse'			=> null  ,
			'base_model'		=> null  ,
			'polymorphic'		=> false 
		);
		
		$options = array_merge($default, $options);
		
		parent::__construct($options);
		
		if ( $options['polymorphic'] ) {
			$this->_model 		= PolyMorphic;
			$this->_base_model  = $options['base_model'];
		}
		
		$this->_type_field = $options['type_field'];
		
		$this->_inverse	   = $options['inverse'];
		
		if ( $this->_inverse )
			$this->setInverseRelationship( $this->_inverse );
		
	}
	
	/**
	 * Serialize an entity to db value
	 * @return 
	 * @param $entity Object
	 */	
	public function serialize($entity)
	{		
		$serialized = array();
		
		$parent_key = $this->getParentKey();
						
		$serialized[$this->getChildKey()] = $entity && is_object($entity) ? $entity->$parent_key : null;
		
		if ( $this->getModel() == PolyMorphic ) {

			if ( $entity instanceof AnDomainProxyEntity ) {
				$entity = $entity->getObject();
			}
			
			$serialized[$this->_type_field] = $entity && is_object($entity) ? (string) $entity->getIdentifier() : null;
		}
		
		return $serialized;
	}
	
	/*	
	public function joinQuery($query)
	{
		$parent_key = $this->targetMapper()->getProperty( $this->getParentKey() )->getField();
		
		$child_key  = $this->getChildKey();
		
		$query->join[] = array(
			'type' => 'INNER',
			'condition' => array("$parent_key = $child_key"),
			'table' 	=> $this->query()->from[0]
		);
				
		$query->update( $this->query() );
	}*/
	
	public function getTableValues( $value )
	{
		$parent_key = $this->getParentKey();
		
		$values 	= array();
		
		if ( is_object($value) )
			$values[] = $value->$parent_key;
		else
			$values[] = null;
		
		if ( $this->getModel() == PolyMorphic ) {
			if ( is_object($value) )
				$values[] = (string) $value->getIdentifier();
			else
				$values[] = null;
		}

		return $values;
	}
	
	public function setInverseRelationship( $name = null )
	{	
		if ( !is_string($name) ) {	
			$identifier = new KIdentifier($this->getMapper()->getModelIdentifier());
			$name 		= KInflector::pluralize($identifier->name);
		}

		$target_mapper = $this->getTargetMapper();

		$target_mapper->has('many', $name, 
			array('model'=>$this->getMapper()->getModelIdentifier(), 'child_key'=>$this->getName()));
	
	}
	
	/**
	 * 
	 * @return 
	 * @param $model Object
	 */
	public function setModel($model)
	{
		parent::setModel($model);
		
		if ( $this->_inverse )
			$this->setInverseRelationship( $this->_inverse );
		return $this;
	}
		
	public function getTableFields()
	{
		$fields =  array( $this->getChildKey() );
		
		if ( $this->getModel() == PolyMorphic ) {
			$fields[] = $this->_type_field;
		}
		
		return $fields;
	}
	
	/**
	 * Materialize an entity using the data from db
	 * @return 
	 * @param $instance Object
	 * @param $data Object
	 */
	public function materialize($instance, $data)
	{	
		$child = $this->getChildKey();
		
		$child = str_replace('.','_', $child);
		
		if ( $this->getModel() == PolyMorphic  ) {
			$model = @$data[str_replace('.','_', $this->_type_field)];
		} else 
			$model = $this->getModel();
		
		$parent_key_value = @$data[$child];
		
		$options = array(
			'model' 	  => $model ,
			'parent_key'  => $this->getParentKey(),
			'parent_key_value'=> $parent_key_value
		);
		
		//if ( !$conditions['id'] ) return;
		return KFactory::tmp('lib.anahita.domain.proxy.entity', $options);
	}
	
	public function getField()
	{
		return $this->getChildKey();
	}
	
	public function validatePresenceOf($value)
	{	
		$serialized = $this->serialize( $value );
		return !is_null( array_shift($serialized) );
	}
	
	/**
	 * 
	 * @return 
	 */	
	public function getAttrTree()
	{	
			
		$tree = array(
			$this->getName() => array(
				$this->getParentKey() => $this->getChildKey()
			)
		);
		
		return $tree;
	}	
	
	/**
	 * Get the target mapper
	 * 
	 * @return Mapper
	 */	
	public function getTargetMapper()
	{	
		if ( $this->getModel() == PolyMorphic ) 
			$model = $this->_base_model;
		else
			$model = $this->getModel();
		
		if ( !$model ) {
			throw new UnexpectedValueException($this->getName().' relationship requires a target model or target base model');
		}
		
		return KFactory::get('lib.anahita.domain.factory.mapper')->get($model);
	}	
}
Return current item: Anahita Social Engine