Skip to content

Commit

Permalink
Fixed propelorm#613. Proves the issue described in propelorm#613 and …
Browse files Browse the repository at this point in the history
…added a kind of fix for it.
  • Loading branch information
marcj committed Feb 25, 2013
1 parent a511f3c commit 9019d7a
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
27 changes: 27 additions & 0 deletions runtime/lib/Propel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
55 changes: 55 additions & 0 deletions test/testsuite/misc/Issue613/Issue613Test.php
Original file line number Diff line number Diff line change
@@ -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');
}

}
22 changes: 22 additions & 0 deletions test/testsuite/misc/Issue613/unserialize_criteria.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9019d7a

Please sign in to comment.