This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for eager loading domain objects via include()
- Loading branch information
Showing
11 changed files
with
321 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Pheasant; | ||
|
||
/** | ||
* A cache for rows that are to be hydrated to objects | ||
*/ | ||
interface Cache | ||
{ | ||
/** | ||
* @return bool | ||
*/ | ||
public function has($hash); | ||
|
||
/** | ||
* Gets a row from the cache, or returns false | ||
* @return array | ||
*/ | ||
public function get($hash); | ||
|
||
/** | ||
* Add or override a row in the cache. Expects a DomainObject | ||
*/ | ||
public function add($object); | ||
|
||
/** | ||
* Clears the entire cache | ||
*/ | ||
public function clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Pheasant\Cache; | ||
|
||
use \Pheasant\Identity; | ||
|
||
/** | ||
* An in-memory array backed cache | ||
*/ | ||
class ArrayCache implements \Pheasant\Cache | ||
{ | ||
private $_cache=array(); | ||
|
||
public function has($hash) | ||
{ | ||
return isset($this->_cache[(string) $hash]); | ||
} | ||
|
||
public function get($hash) | ||
{ | ||
// hack to avoid an extra isset check | ||
$value = @$this->_cache[(string) $hash]; | ||
|
||
return $value ? $value : false; | ||
} | ||
|
||
public function add($object) | ||
{ | ||
$this->_cache[(string) $object->identity()] = $object->toArray(); | ||
} | ||
|
||
public function clear() | ||
{ | ||
$this->_cache = array(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,23 @@ | |
*/ | ||
class BelongsTo extends HasOne | ||
{ | ||
private $_property; | ||
|
||
/* (non-phpdoc) | ||
* @see Relationship::get() | ||
*/ | ||
public function get($object, $key) | ||
public function get($object, $key, $cache=null) | ||
{ | ||
if(($localValue = $object->{$this->local}) === null) | ||
if ($cache) { | ||
$schema = \Pheasant::instance()->schema($this->class); | ||
if ($cached = $cache->get($schema->hash($object, array($this->local)))) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lox
Author
Owner
|
||
return $schema->hydrate($cached); | ||
} | ||
} | ||
|
||
if (($localValue = $object->{$this->local}) === null) { | ||
return null; | ||
} | ||
|
||
return $this->hydrate($this->query("{$this->foreign}=?", $localValue) | ||
->execute()->row()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Pheasant\Relationships; | ||
|
||
use \Pheasant\Query\Criteria; | ||
use \Pheasant\Cache\ArrayCache; | ||
|
||
/** | ||
* Finds all possible objects in a relationship that might exist in a query | ||
* and queries them in one shot for future hydration | ||
* @see http://stackoverflow.com/questions/97197/what-is-the-n1-selects-issue | ||
*/ | ||
class Includer | ||
{ | ||
private | ||
$_query, | ||
$_rel, | ||
$_cache | ||
; | ||
|
||
public function __construct($query, $rel) | ||
{ | ||
$this->_query = $query; | ||
$this->_rel = $rel; | ||
} | ||
|
||
public function loadCache() | ||
{ | ||
$this->_cache = new ArrayCache(); | ||
$ids = iterator_to_array( | ||
$this->_query->select('DISTINCT '.$this->_rel->local)->execute()->column() | ||
); | ||
|
||
$relatedObjects = \Pheasant::instance() | ||
->finderFor($this->_rel->class) | ||
->find($this->_rel->class, new Criteria( | ||
$this->_rel->foreign.'=?', array($ids)) | ||
); | ||
|
||
foreach ($relatedObjects as $obj) { | ||
$this->_cache->add($obj); | ||
} | ||
} | ||
|
||
public function get($object, $key) | ||
{ | ||
if(!isset($this->_cache)) { | ||
$this->loadCache(); | ||
} | ||
|
||
return $this->_rel->get($object, $key, $this->_cache); | ||
} | ||
} |
Oops, something went wrong.
Given
Veldwerkdag -- belongsTo (projectID, id) --> Project
$schema->hash($object, array($this->local))
results inModel\Project[projectID=x]
as where items in the cache are stored asModel\Project[id=x]
.At first I thought replacing
$this->local
with$this->foreign
fixed the issue, but I guess this is a bug inSchema::hash()
instead.