<?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 AnDomainDescriptionRelationshipManytomany extends AnDomainDescriptionRelationshipOnetomany
{
protected $_target_parent_key;
protected $_target_child_key;
protected $_intersection;
protected $_fields = null;
protected $_merged_queries = array();
/**
*
* @return
* @param $options Object
*/
public function __construct($options)
{
if ( !isset($options['parent_key']) )
$options['parent_key'] = 'id';
parent::__construct($options);
if ( !isset($options['target_parent_key']) )
$options['target_parent_key'] = 'id';
$this->_target_parent_key = $options['target_parent_key'];
$this->_target_child_key = $options['target_child_key'];
try {
$this->_intersection = new KIdentifier($options['through']);
} catch(Exception $e) {
$this->_intersection = '#__'.$options['through'];
}
}
/**
*
* @return
* @param $query Object
* @param $property Object
* @param $options Object
*/
public function mergeQueryWithTargetQuery($query, $property, $options)
{
$this->_initializeKeys();
$int_table = $this->_intersection;
if ( is_object($int_table) ) {
$int_query = $this->_intersection->getQuery();
if ( isset($options['where']) ) {
$where = $options['where'];
$int_query->where($this->getTargetChildKey(), $where['constraint'], $where['value'], $where['condition']);
}
$query->update( $int_query );
$int_table = $int_query->from[0];
}
$condition = $query->getAdapter()->quoteName($this->_fields['child_key'].' = '.$this->_fields['parent_key']);
$query->join[] = array(
'table' => $int_table ,
'type' => 'INNER',
'condition'=> array($condition)
);
$query->distinct();
}
/**
*
* @return
*/
protected function _initializeKeys()
{
if ( !isset($this->_fields) ) {
if ( is_object($this->_intersection) ) {
$this->_intersection = KFactory::get('lib.anahita.domain.factory.mapper')->get( $this->_intersection );
$this->_fields['child_key'] = $this->getChildKey();
if ( $property = $this->_intersection->getProperty($this->getChildKey()) ) {
$this->_fields['child_key'] = $property->getChildKey();
}
$this->_fields['target_child_key'] = $this->_target_child_key;
if ( $property = $this->_intersection->getProperty($this->_target_child_key) ) {
$this->_fields['target_child_key'] = $property->getChildKey();
}
} else {
$this->_fields['target_child_key'] = $this->_target_child_key;
$this->_fields['child_key'] = $this->getChildKey();
}
$this->_fields['target_parent_key'] = $this->_target_parent_key;
if ( $property = $this->getTargetMapper()->getProperty( $this->_target_parent_key ) ) {
$this->_fields['target_parent_key'] = $property->getField();
}
$this->_fields['parent_key'] = $this->getParentKey();
if ( $property = $this->_mapper->getProperty($this->getParentKey()) ) {
$this->_fields['parent_key'] = $property->getField();
}
}
}
/**
*
* @return
*/
public function getIntersection()
{
return $this->_intersection;
}
/**
*
* @return
*/
public function getTargetParentKey()
{
return $this->_target_parent_key;
}
/**
*
* @return
*/
public function getTargetChildKey()
{
return $this->_target_child_key;
}
/**
*
* @return
* @param $instance Object
* @param $data Object
*/
public function materialize($instance, $data)
{
$this->_initializeKeys();
$target_query = clone $this->getTargetQuery();
$this->_evalQuery($target_query, $instance, $data);
$parent_key = str_replace('.','_', $this->_fields['parent_key']);
$parent_key_value = @$data[$parent_key];
$int_table = $this->_intersection;
if ( is_object($int_table) ) {
$target_query->update( $int_query = $this->_intersection->getQuery() );
$int_table = $int_query->from[0];
}
$target_query->join[] = array(
'table' => $int_table ,
'type' => 'INNER',
'condition'=> array($this->_fields['target_child_key'].'='.$this->_fields['target_parent_key'])
);
$target_query->where($this->_fields['child_key'],'=',$parent_key_value);
$target_query->distinct();
$collection = KFactory::tmp($this->_collection, array('query'=>$target_query));
$proxy = KFactory::tmp('lib.anahita.domain.proxy.collection', array(
'collection' => $collection
));
return $proxy;
}
/**
*
* @return
*/
public function getTargetQuery()
{
if ( !isset($this->_query) ) {
$this->_query = KFactory::get('lib.anahita.domain.factory.query')->get($this->getModel());
}
return $this->_query;
}
}