Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #613. Proves the issue described in #613 and added a kind of fi…

…x for it.
  • Loading branch information...
commit 9019d7a68c7ed7289c1a78ccd09bfda7dd534cdf 1 parent a511f3c
@marcj authored
View
27 runtime/lib/Propel.php
@@ -381,6 +381,33 @@ public static function getConfiguration($type = PropelConfiguration::TYPE_ARRAY)
return self::$configuration->getParameters($type);
}
+
+ /**
+ * This makes sure all model peers have registered their table to Propel's core.
+ * E.g. through a `unserialization` of a standalone Criteria it's not automatically
+ * known which tables belongs to which model classes.
+ *
+ * This call can be quite slow and can have a big memory consumption, since
+ * it autoLoads/parses all model class that belongs to the current database
+ * (defined through `Propel::setConfiguration` or `Propel::init`).
+ *
+ * If you know which tables (and their class name) are used in the Criteria,
+ * then you should better trigger only the appropriate `*Peer::buildTableMap()`
+ * to make the `table => model class` known. If not, this method is the only
+ * wait to make all `tables to model classes` relation available to Propel.
+ *
+ */
+ public static function buildAllTableMaps()
+ {
+ if (self::$configuration && self::$configuration['classmap']) {
+ foreach (self::$configuration['classmap'] as $class => $file) {
+ if (substr($class, -4) === 'Peer') {
+ class_exists($class);
+ }
+ }
+ }
+ }
+
/**
* Override the configured logger.
*
View
55 test/testsuite/misc/Issue613/Issue613Test.php
@@ -0,0 +1,55 @@
+<?php
+
+require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreTestBase.php';
+
+/**
+ * Proves the issue described in #613 and the fix for it.
+ *
+ * Basically it describes that a standalone Criteria does not
+ * contain the information which table belongs to which model class.
+ * We've added with this issue a new method `Propel::buildAllTableMaps` which
+ * can help. See the description of this method for more information.
+ *
+ * @see https://github.com/propelorm/Propel/issues/613
+ */
+class Issue613Test extends BookstoreTestBase
+{
+
+ /**
+ * Creates a
+ *
+ * @return string The file path to the serialized Criteria
+ */
+ private function getPreparedCriteriaObject()
+ {
+ $tmpFile = sys_get_temp_dir().'propel-test-issue-613-serialized-object.tmp';
+ BookPeer::clearRelatedInstancePool();
+ $criteria = new Criteria();
+ $criteria->addSelectColumn(BookPeer::ID);
+ $criteria->addSelectColumn(AuthorPeer::LAST_NAME);
+ $criteria->add(BookPeer::ID, 1);
+ $criteria->addJoin(BookPeer::AUTHOR_ID, AuthorPeer::ID);
+ file_put_contents($tmpFile, serialize($criteria));
+ return $tmpFile;
+ }
+
+ private function fireTestUnserialization($pScript)
+ {
+
+ $tmpFile = $this->getPreparedCriteriaObject();
+ $returnCode = 0;
+
+ $cmd = 'php '.escapeshellcmd(__DIR__.'/'.$pScript) . ' ' . escapeshellarg($tmpFile);
+ system($cmd, $returnCode);
+
+ unlink($tmpFile);
+ $this->assertEquals(0, $returnCode, 'The unserialization should work without exception.');
+ }
+
+ public function testUnserializeCriteria()
+ {
+ $this->fireTestUnserialization('unserialize_criteria.php');
+ $this->fireTestUnserialization('unserialize_criteria_buildAllTableMaps.php');
+ }
+
+}
View
22 test/testsuite/misc/Issue613/unserialize_criteria.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * This php file tries to load a serialized Criteria, to unserialize it and make the table to model class relation
+ * known through `BookPeer::buildTableMap`
+ */
+
+require dirname(__FILE__) . '/../../../../vendor/autoload.php';
+set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/../../../fixtures/bookstore/build/classes'));
+Propel::init(__DIR__ . '/../../../fixtures/bookstore/build/conf/bookstore-conf.php');
+
+$phpSerializeString = file_get_contents($argv[1]);
+
+$criteria = unserialize($phpSerializeString);
+BookPeer::buildTableMap();
+
+$test = BasePeer::doSelect($criteria);
+if ($test->queryString !== 'SELECT book.id, author.last_name FROM `book` INNER JOIN `author` ON (book.author_id=author.id) WHERE book.id=:p1') {
+ exit(1);
+}
+
+exit(0); //Anything went fine
View
22 test/testsuite/misc/Issue613/unserialize_criteria_buildAllTableMaps.php
@@ -0,0 +1,22 @@
+<?php
+
+/**
+ * This php file tries to load a serialized Criteria, to unserialize it and make the table to model class relation
+ * known through `Propel::buildAllTableMaps`
+ */
+
+require dirname(__FILE__) . '/../../../../vendor/autoload.php';
+set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__) . '/../../../fixtures/bookstore/build/classes'));
+Propel::init(__DIR__ . '/../../../fixtures/bookstore/build/conf/bookstore-conf.php');
+
+$phpSerializeString = file_get_contents($argv[1]);
+
+$criteria = unserialize($phpSerializeString);
+Propel::buildAllTableMaps();
+
+$test = BasePeer::doSelect($criteria);
+if ($test->queryString !== 'SELECT book.id, author.last_name FROM `book` INNER JOIN `author` ON (book.author_id=author.id) WHERE book.id=:p1') {
+ exit(1);
+}
+
+exit(0); //Anything went fine
Please sign in to comment.
Something went wrong with that request. Please try again.