Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

Commit

Permalink
* bugfix: formerly only one refered object per model class could be c…
Browse files Browse the repository at this point in the history
…ached.
  • Loading branch information
caefer committed Oct 12, 2010
1 parent 41554ec commit 71f79e2
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions lib/Doctrine/Template/LooselyCoupleable.php
@@ -1,7 +1,7 @@
<?php
class Doctrine_Template_LooselyCoupleable extends Doctrine_Template
{
protected $Object;
protected $_objectCache = array();

public function setTableDefinition()
{
Expand All @@ -13,28 +13,47 @@ public function getObject()
{
$record = $this->getInvoker();

if(isset($this->Object))
if(false !== ($object = $this->getCachedObject($record->obj_type, $record->obj_pk)))
{
return $this->Object;
return $object;
}
else if(isset($record->obj_type) && isset($record->obj_pk))
{
$this->Object = Doctrine_Core::getTable($record->obj_type)->find($record->obj_pk);
return $this->Object;
$object = Doctrine_Core::getTable($record->obj_type)->find($record->obj_pk);
$this->setCachedObject($record->obj_type, $record->obj_pk, $object);
return $object;
}
else
{
return null;
}

}

public function setObject($object)
{
$record = $this->getInvoker();
$record->obj_type = $this->_findObjectType($object);
$record->obj_pk = $this->_findObjectPrimaryKey($object);
$this->Object = $object;

$this->setCachedObject($record->obj_type, $record->obj_pk, $object);
}

public function getCachedObject($type, $pk)
{
if(array_key_exists($type, $this->_objectCache) && array_key_exists($pk, $this->_objectCache[$type]))
{
return $this->_objectCache[$type][$pk];
}
return false;
}

public function setCachedObject($type, $pk, $object)
{
if(!array_key_exists($type, $this->_objectCache))
{
$this->_objectCache[$type] = array();
}
$this->_objectCache[$type][$pk] = $object;
}

protected function _findObjectType($object)
Expand Down

0 comments on commit 71f79e2

Please sign in to comment.