propelorm/Propel2

View on GitHub
templates/Builder/Om/tableMapInstancePool.php

Summary

Maintainability
A
0 mins
Test Coverage

    /**
     * Adds an object to the instance pool.
     *
     * Propel keeps cached copies of objects in an instance pool when they are retrieved
     * from the database. In some cases you may need to explicitly add objects
     * to the cache in order to ensure that the same objects are always returned by find*()
     * and findPk*() calls.
     *
     * @param <?=    $objectClassName ?> $obj A <?= $objectClassName ?> object.
     * @param string|null $key Key (optional) to use for instance map (for performance boost if key was already calculated externally).
     *
     * @return void
     */
    public static function addInstanceToPool(<?= substr($objectClassName, strrpos( $objectClassName, '\\' ) + 1 ) ?> $obj, ?string $key = null): void
    {
        if (Propel::isInstancePoolingEnabled()) {
            if (null === $key) {
                $key = <?= $addInstancePoolKeySnippet ?>;
            } // if key === null
            self::$instances[$key] = $obj;
        }
    }

    /**
     * Removes an object from the instance pool.
     *
     * Propel keeps cached copies of objects in an instance pool when they are retrieved
     * from the database.  In some cases -- especially when you override doDelete
     * methods in your stub classes -- you may need to explicitly remove objects
     * from the cache in order to prevent returning objects that no longer exist.
     *
     * @param mixed $value A <?= $objectClassName ?> object or a primary key value.
     *
     * @return void
     */
    public static function removeInstanceFromPool($value): void
    {
        if (Propel::isInstancePoolingEnabled() && null !== $value) {
            if (is_object($value) && $value instanceof <?= $objectClassName ?>) {
                $key = <?= $removeInstancePoolKeySnippetObjects ?>;

            } elseif (is_array($value) && count($value) === <?= $countPks ?>) {
                // assume we've been passed a primary key";
                $key = <?= $removeInstancePoolKeySnippetPks ?>;
            } elseif ($value instanceof Criteria) {
                self::$instances = [];

                return;
            } else {
                $e = new PropelException("Invalid value passed to removeInstanceFromPool().  Expected primary key or <?= $objectClassName ?> object; got " . (is_object($value) ? get_class($value) . ' object.' : var_export($value, true)));
                throw $e;
            }

            unset(self::$instances[$key]);
        }
    }