public static function getInstanceFromPool($key) { if (Propel::isInstancePoolingEnabled()) { if (isset(self::$instances[$key])) { return self::$instances[$key]; } } return null; }
public function testInstancePoolingReenabled() { Propel::enableInstancePooling(); $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find($this->con); foreach ($books as $book) { } $this->assertTrue(Propel::isInstancePoolingEnabled()); Propel::disableInstancePooling(); $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find($this->con); foreach ($books as $book) { } $this->assertFalse(Propel::isInstancePoolingEnabled()); Propel::enableInstancePooling(); }
public function testFormatterDoesNotReenableInstancePoolIfItWasInitiallyDisabled() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); BookstoreDataPopulator::depopulate($con); Propel::disableInstancePooling(); $stmt = $con->query('SELECT * FROM book'); $formatter = new OnDemandFormatter(); $formatter->init(new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book')); $this->assertFalse(Propel::isInstancePoolingEnabled()); $books = $formatter->format($stmt); $this->assertFalse(Propel::isInstancePoolingEnabled()); $books->getIterator()->closeCursor(); $this->assertFalse(Propel::isInstancePoolingEnabled()); Propel::enableInstancePooling(); }
public static function getInstanceFromPool($key) { $object = static::defaultGetInstanceFromPool($key); if (Propel::isInstancePoolingEnabled()) { if (empty($object) === true) { $object = GlobalCache::get(static::getPrefix() . $key); } if ($object === false) { $object = null; } else { static::defaultAddInstanceToPool($object, $key); } } return $object; }
/** * Populate the database using all the Entity classes previously added. * * @param PropelPDO $con A Propel connection object * * @return array A list of the inserted PKs */ public function execute($con = null) { if (null === $con) { $con = $this->getConnection(); } $isInstancePoolingEnabled = Propel::isInstancePoolingEnabled(); Propel::disableInstancePooling(); $insertedEntities = array(); $con->beginTransaction(); foreach ($this->quantities as $class => $number) { for ($i = 0; $i < $number; $i++) { $insertedEntities[$class][] = $this->entities[$class]->execute($con, $insertedEntities); } } $con->commit(); if ($isInstancePoolingEnabled) { Propel::enableInstancePooling(); } return $insertedEntities; }
/** * 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 \Diaporamas\Model\DiaporamaVersion object or a primary key value. */ public static function removeInstanceFromPool($value) { if (Propel::isInstancePoolingEnabled() && null !== $value) { if (is_object($value) && $value instanceof \Diaporamas\Model\DiaporamaVersion) { $key = serialize(array((string) $value->getId(), (string) $value->getVersion())); } elseif (is_array($value) && count($value) === 2) { // assume we've been passed a primary key"; $key = serialize(array((string) $value[0], (string) $value[1])); } elseif ($value instanceof Criteria) { self::$instances = []; return; } else { $e = new PropelException("Invalid value passed to removeInstanceFromPool(). Expected primary key or \\Diaporamas\\Model\\DiaporamaVersion object; got " . (is_object($value) ? get_class($value) . ' object.' : var_export($value, true))); throw $e; } unset(self::$instances[$key]); } }
public function testSetUp() { $this->assertTrue(Propel::isInstancePoolingEnabled()); $this->assertEquals(2, count($this->author->getBooks())); $this->assertEquals(2, $this->author->countBooks()); }
/** * 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 \App\Propel\VariationTypeI18n object or a primary key value. */ public static function removeInstanceFromPool($value) { if (Propel::isInstancePoolingEnabled() && null !== $value) { if (is_object($value) && $value instanceof \App\Propel\VariationTypeI18n) { $key = serialize([null === $value->getVariationTypeId() || is_scalar($value->getVariationTypeId()) || is_callable([$value->getVariationTypeId(), '__toString']) ? (string) $value->getVariationTypeId() : $value->getVariationTypeId(), null === $value->getLocale() || is_scalar($value->getLocale()) || is_callable([$value->getLocale(), '__toString']) ? (string) $value->getLocale() : $value->getLocale()]); } elseif (is_array($value) && count($value) === 2) { // assume we've been passed a primary key"; $key = serialize([null === $value[0] || is_scalar($value[0]) || is_callable([$value[0], '__toString']) ? (string) $value[0] : $value[0], null === $value[1] || is_scalar($value[1]) || is_callable([$value[1], '__toString']) ? (string) $value[1] : $value[1]]); } elseif ($value instanceof Criteria) { self::$instances = []; return; } else { $e = new PropelException("Invalid value passed to removeInstanceFromPool(). Expected primary key or \\App\\Propel\\VariationTypeI18n object; got " . (is_object($value) ? get_class($value) . ' object.' : var_export($value, true))); throw $e; } unset(self::$instances[$key]); } }
/** * Makes an additional query to populate the objects related to the collection objects * by a certain relation * * @param string $relation Relation name (e.g. 'Book') * @param Criteria $criteria Optional Criteria object to filter the related object collection * @param ConnectionInterface $con Optional connection object * * @return ObjectCollection The list of related objects */ public function populateRelation($relation, $criteria = null, $con = null) { if (!Propel::isInstancePoolingEnabled()) { throw new RuntimeException(__METHOD__ . ' needs instance pooling to be enabled prior to populating the collection'); } $relationMap = $this->getFormatter()->getTableMap()->getRelation($relation); if ($this->isEmpty()) { // save a useless query and return an empty collection $coll = new ObjectCollection(); $coll->setModel($relationMap->getRightTable()->getClassName()); return $coll; } $symRelationMap = $relationMap->getSymmetricalRelation(); $query = PropelQuery::from($relationMap->getRightTable()->getClassName()); if (null !== $criteria) { $query->mergeWith($criteria); } // query the db for the related objects $filterMethod = 'filterBy' . $symRelationMap->getName(); $relatedObjects = $query->{$filterMethod}($this)->find($con); if (RelationMap::ONE_TO_MANY === $relationMap->getType()) { // initialize the embedded collections of the main objects $relationName = $relationMap->getName(); foreach ($this as $mainObj) { $mainObj->initRelation($relationName); } // associate the related objects to the main objects $getMethod = 'get' . $symRelationMap->getName(); $addMethod = 'add' . $relationName; foreach ($relatedObjects as $object) { $mainObj = $object->{$getMethod}(); // instance pool is used here to avoid a query $mainObj->{$addMethod}($object); } } elseif (RelationMap::MANY_TO_ONE === $relationMap->getType()) { // nothing to do; the instance pool will catch all calls to getRelatedObject() // and return the object in memory } else { throw new UnsupportedRelationException(__METHOD__ . ' does not support this relation type'); } return $relatedObjects; }
/** * Reload all already loaded nodes to sync them with updated db * * @param ChildCategory $prune Object to prune from the update * @param ConnectionInterface $con Connection to use. */ public static function updateLoadedNodes($prune = null, ConnectionInterface $con = null) { if (Propel::isInstancePoolingEnabled()) { $keys = array(); /** @var $obj ChildCategory */ foreach (CategoryTableMap::$instances as $obj) { if (!$prune || !$prune->equals($obj)) { $keys[] = $obj->getPrimaryKey(); } } if (!empty($keys)) { // We don't need to alter the object instance pool; we're just modifying these ones // already in the pool. $criteria = new Criteria(CategoryTableMap::DATABASE_NAME); $criteria->add(CategoryTableMap::COL_CATEGORY_ID, $keys, Criteria::IN); $dataFetcher = ChildCategoryQuery::create(null, $criteria)->setFormatter(ModelCriteria::FORMAT_STATEMENT)->find($con); while ($row = $dataFetcher->fetch()) { $key = CategoryTableMap::getPrimaryKeyHashFromRow($row, 0); /** @var $object ChildCategory */ if (null !== ($object = CategoryTableMap::getInstanceFromPool($key))) { $object->setLeftValue($row[7]); $object->setRightValue($row[8]); $object->setLevel($row[9]); $object->clearNestedSetChildren(); } } $dataFetcher->close(); } } }