public function testFormat() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); BookstoreEmployeePeer::clearInstancePool(); $stmt = $con->query('SELECT * FROM bookstore_employee'); $formatter = new PropelObjectFormatter(); $formatter->init(new ModelCriteria('bookstore', 'BookstoreEmployee')); $emps = $formatter->format($stmt); $expectedClass = array('b1' => 'BookstoreEmployee', 'b2' => 'BookstoreManager', 'b3' => 'BookstoreCashier'); foreach ($emps as $emp) { $this->assertEquals($expectedClass[$emp->getName()], get_class($emp), 'format() creates objects of the correct class when using inheritance'); } }
public function init(ModelCriteria $criteria) { parent::init($criteria); $this->isSingleTableInheritance = $criteria->getTableMap()->isSingleTableInheritance(); return $this; }
public function testFormatOneNoResult() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); $formatter = new PropelObjectFormatter(); $formatter->init(new ModelCriteria('bookstore', 'Book')); $book = $formatter->formatOne($stmt); $this->assertNull($book, 'PropelObjectFormatter::formatOne() returns null when no result'); }
public function testFormaWithRelatedObjects() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $con->useDebug(false); $con->useDebug(true); $this->assertEquals(0, $con->getQueryCount()); $stmt = $con->query('SELECT * FROM author LEFT JOIN book ON (author.id = book.author_id)'); $formatter = new PropelObjectFormatter(); $criteria = new ModelCriteria('bookstore', 'Author'); $criteria->joinWith('Book'); $formatter->init($criteria); $authors = $formatter->format($stmt); $this->assertEquals(1, $con->getQueryCount()); $this->assertTrue($authors instanceof PropelObjectCollection, 'PropelObjectFormatter::formatOne() returns a model object'); foreach ($authors as $author) { $this->assertTrue($author->getBooks() instanceof PropelCollection); if ('Grass' === $author->getLastName()) { $this->assertEquals(2, $author->countBooks()); } else { $this->assertEquals(1, $author->countBooks()); } } $this->assertEquals(1, $con->getQueryCount()); }