Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merged PR #177.

Fixes #90
  • Loading branch information...
commit 6984ed01cc923dae9ed86b3b3dafd4cabf3f203c 2 parents a77a4cf + 1acaf0c
@willdurand willdurand authored
View
10 runtime/lib/formatter/PropelFormatter.php
@@ -169,12 +169,14 @@ protected function isWithOneToMany()
*/
protected function getWorkerObject($col, $class)
{
- if(isset($this->currentObjects[$col])) {
- $this->currentObjects[$col]->clear();
+ $key = $col . '_' . $class;
+
+ if (isset($this->currentObjects[$key])) {
+ $this->currentObjects[$key]->clear();
} else {
- $this->currentObjects[$col] = new $class();
+ $this->currentObjects[$key] = new $class();
}
- return $this->currentObjects[$col];
+ return $this->currentObjects[$key];
}
/**
View
65 test/testsuite/runtime/formatter/PropelFormatterTest.php
@@ -0,0 +1,65 @@
+<?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
+ */
+
+require_once dirname(__FILE__) . '/../../../tools/helpers/bookstore/BookstoreEmptyTestBase.php';
+
+/**
+ * Test class for PropelObjectFormatter.
+ *
+ * @author Francois Zaninotto
+ * @version $Id$
+ * @package runtime.formatter
+ */
+class PropelFormatterTest extends BookstoreEmptyTestBase
+{
+ protected function setUp()
+ {
+ parent::setUp();
+ BookstoreDataPopulator::populate();
+ }
+
+ public function testGetWorkerObjectReturnsRightClass()
+ {
+ $formatter = $this->getMockForAbstractClass('PropelFormatter');
+
+ $method = new ReflectionMethod('PropelFormatter', 'getWorkerObject');
+ $method->setAccessible(true);
+
+ $classNames = array(
+ 'Bookstore',
+ 'BookReader',
+ 'BookClubList',
+ );
+
+ $col = 0;
+ foreach ($classNames as $className) {
+ // getWorkerObject() should always return an instance of the requested class, regardless of the value of $col
+ $result = $method->invoke($formatter, $col, $className);
+
+ $this->assertEquals($className, get_class($result), 'getWorkerObject did not return an instance of the requested class');
+ }
+ }
+
+ public function testGetWorkerObjectCachedInstance()
+ {
+ $formatter = $this->getMockForAbstractClass('PropelFormatter');
+
+ $method = new ReflectionMethod('PropelFormatter', 'getWorkerObject');
+ $method->setAccessible(true);
+
+ $className = 'Bookstore';
+ $col = 0;
+
+ $result1 = $method->invoke($formatter, $col, $className);
+ $result2 = $method->invoke($formatter, $col, $className);
+
+ $this->assertEquals(spl_object_hash($result1), spl_object_hash($result2), 'getWorkerObject should return a cached instance of a class at the same col index');
+ }
+}
View
21 test/testsuite/runtime/formatter/PropelOnDemandFormatterTest.php
@@ -148,4 +148,25 @@ public function testFormatOneManyResults()
$this->assertTrue($book instanceof Book, 'PropelOnDemandFormatter::formatOne() returns a model object');
}
+ public function testFormatSingleTableInheritanceManyResults()
+ {
+ $con = Propel::getConnection(BookPeer::DATABASE_NAME);
+ BookstoreDataPopulator::populate($con);
+
+ $stmt = $con->query('SELECT * FROM bookstore_employee');
+ $formatter = new PropelOnDemandFormatter();
+ $formatter->init(new ModelCriteria('bookstore', 'BookstoreEmployee'));
+
+ $employees = $formatter->format($stmt);
+
+ foreach ($employees as $employee) {
+ $row = array();
+ $row[1] = $employee->getClassKey();
+
+ $omClass = BookstoreEmployeePeer::getOMClass($row, 0, false);
+ $actualClass = get_class($employee);
+
+ $this->assertEquals($omClass, $actualClass, 'PropelOnDemandFormatter::format() should handle single table inheritance');
+ }
+ }
}
View
5 test/tools/helpers/bookstore/BookstoreDataPopulator.php
@@ -167,6 +167,11 @@ public static function populate($con = null)
$bemp2->setSupervisor($bemp1);
$bemp2->save($con);
+ $bemp3 = new BookstoreCashier();
+ $bemp3->setName("Tim");
+ $bemp3->setJobTitle("Cashier");
+ $bemp3->save($con);
+
$role = new AcctAccessRole();
$role->setName("Admin");
Please sign in to comment.
Something went wrong with that request. Please try again.