Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Propel package. | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @license MIT License | ||
*/ | ||
|
||
namespace Propel\Runtime\ActiveQuery; | ||
|
||
use Propel\Runtime\Propel; | ||
use Propel\Runtime\Exception\RuntimeException; | ||
|
||
trait InstancePoolTrait | ||
{ | ||
private static $instances = array(); | ||
|
||
public static function addInstanceToPool($object, $key = null) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
staabm
Member
|
||
{ | ||
if (Propel::isInstancePoolingEnabled()) { | ||
if (null === $key) { | ||
$key = (string) $object->getId(); | ||
} | ||
|
||
self::$instances[$key] = $object; | ||
} | ||
} | ||
|
||
public static function removeInstanceFromPool($value) | ||
{ | ||
if (Propel::isInstancePoolingEnabled() && null !== $value) { | ||
if (is_object($value)) { | ||
$key = (string) $value->getId(); | ||
} elseif (is_scalar($value)) { | ||
// assume we've been passed a primary key | ||
$key = (string) $value; | ||
} else { | ||
throw new RuntimeException('Invalid value passed to removeInstanceFromPool()'); | ||
} | ||
|
||
unset(self::$instances[$key]); | ||
} | ||
} | ||
|
||
public static function getInstanceFromPool($key) | ||
{ | ||
if (Propel::isInstancePoolingEnabled()) { | ||
if (isset(self::$instances[$key])) { | ||
return self::$instances[$key]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static function clearInstancePool() | ||
{ | ||
self::$instances = array(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
} |
3 comments
on commit 0a96ef6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding another dimension to the instance pool? The first dimension should be the scope on which you are pooling, and the second dimension would be the current one, the value representing the instance.
This would allow for example to create a pool based on a (composite) unique
key, which is not the primary key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@havvg this is already possible, but you have to handle it by hand ;) you just need to provide the right $key
when adding/getting your object to/from the pool.
$key = 'custom_' . $uniqKey1 . '_' . $uniqKey2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, true - and you need to (over)write the findOneBy*
methods of the *Query. Thanks ;)
you could use
self
as typehint for$object
so php will take care that we don't insert objects from different types into a instance-pool.thx @gharlan for the tip.