/** * This is run after each unit test. It empties the database. */ protected function tearDown() { BookstoreDataPopulator::depopulate(); $this->assertEquals(0, count(BookPeer::doSelect(new Criteria())), "Expect book table to be empty."); $this->assertEquals(0, count(AuthorPeer::doSelect(new Criteria())), "Expect author table to be empty."); $this->assertEquals(0, count(PublisherPeer::doSelect(new Criteria())), "Expect publisher table to be empty."); $this->assertEquals(0, count(ReviewPeer::doSelect(new Criteria())), "Expect review table to be empty."); $this->assertEquals(0, count(MediaPeer::doSelect(new Criteria())), "Expect media table to be empty."); $this->assertEquals(0, count(BookstoreEmployeePeer::doSelect(new Criteria())), "Expect bookstore_employee table to be empty."); $this->assertEquals(0, count(BookstoreEmployeeAccountPeer::doSelect(new Criteria())), "Expect bookstore_employee_account table to be empty."); $this->assertEquals(0, count(BookstoreSalePeer::doSelect(new Criteria())), "Expect bookstore_sale table to be empty."); BookPeer::clearInstancePool(); $this->assertEquals(0, count(BookPeer::$instances), "Expected 0 Book instances after clearInstancePool()"); AuthorPeer::clearInstancePool(); $this->assertEquals(0, count(AuthorPeer::$instances), "Expected 0 Author instances after clearInstancePool()"); PublisherPeer::clearInstancePool(); $this->assertEquals(0, count(PublisherPeer::$instances), "Expected 0 Publisher instances after clearInstancePool()"); ReviewPeer::clearInstancePool(); $this->assertEquals(0, count(ReviewPeer::$instances), "Expected 0 Review instances after clearInstancePool()"); MediaPeer::clearInstancePool(); $this->assertEquals(0, count(MediaPeer::$instances), "Expected 0 Media instances after clearInstancePool()"); BookstoreEmployeePeer::clearInstancePool(); $this->assertEquals(0, count(BookstoreEmployeePeer::$instances), "Expected 0 BookstoreEmployee instances after clearInstancePool()"); BookstoreEmployeeAccountPeer::clearInstancePool(); $this->assertEquals(0, count(BookstoreEmployeeAccountPeer::$instances), "Expected 0 BookstoreEmployeeAccount instances after clearInstancePool()"); BookstoreSalePeer::clearInstancePool(); $this->assertEquals(0, count(BookstoreSalePeer::$instances), "Expected 0 BookstoreSale instances after clearInstancePool()"); parent::tearDown(); }
public function setUp() { parent::setUp(); $a = new Author(); $a->setFirstName("Douglas"); $a->setLastName("Adams"); $b1 = new Book(); $b1->setTitle("The Hitchhikers Guide To The Galaxy"); $a->addBook($b1); $b2 = new Book(); $b2->setTitle("The Restaurant At The End Of The Universe"); $a->addBook($b2); $a->save(); $this->author = $a; $this->books = array($b1, $b2); Propel::enableInstancePooling(); // Clear author instance pool so the object would be fetched from the database AuthorPeer::clearInstancePool(); }
public function testNestedTransactionForceRollBack() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $driver = $con->getAttribute(PDO::ATTR_DRIVER_NAME); // main transaction $con->beginTransaction(); $a = new Author(); $a->setFirstName('Test'); $a->setLastName('User'); $a->save($con); $authorId = $a->getId(); // nested transaction $con->beginTransaction(); $a2 = new Author(); $a2->setFirstName('Test2'); $a2->setLastName('User2'); $a2->save($con); $authorId2 = $a2->getId(); // force rollback $con->forceRollback(); $this->assertEquals(0, $con->getNestedTransactionCount(), 'nested transaction is null after nested transaction forced rollback'); $this->assertFalse($con->isInTransaction(), 'PropelPDO is not in transaction after nested transaction force rollback'); AuthorPeer::clearInstancePool(); $at = AuthorPeer::retrieveByPK($authorId); $this->assertNull($at, "Rolled back transaction is not persisted in database"); $at2 = AuthorPeer::retrieveByPK($authorId2); $this->assertNull($at2, "Forced Rolled back nested transaction is not persisted in database"); }
public function testFindPkWithOneToMany() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $book = BookQuery::create()->findOneByTitle('Harry Potter and the Order of the Phoenix', $con); $pk = $book->getPrimaryKey(); BookPeer::clearInstancePool(); $book = BookQuery::create()->joinWith('Review')->findPk($pk, $con); $count = $con->getQueryCount(); $reviews = $book->getReviews(); $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query '); $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated'); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->filterByTitle('The Tin Drum'); $c->join('Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $c->limit(1); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $books = $c->find($con); foreach ($books as $book) { break; } $this->assertTrue($book instanceof Book, 'withColumn() do not change the resulting model class'); $this->assertEquals('The Tin Drum', $book->getTitle()); $this->assertTrue($book->getAuthor() instanceof Author, 'PropelObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'PropelObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'PropelObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'PropelObjectFormatter correctly hydrates all virtual columns'); }
/** * Test the doSelectJoin*() methods when the related object is NULL. */ public function testDoSelectJoin_NullFk() { $b1 = new Book(); $b1->setTitle("Test NULLFK 1"); $b1->setISBN("NULLFK-1"); $b1->save(); $b2 = new Book(); $b2->setTitle("Test NULLFK 2"); $b2->setISBN("NULLFK-2"); $b2->setAuthor(new Author()); $b2->getAuthor()->setFirstName("Hans")->setLastName("L"); $b2->save(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); $c = new Criteria(); $c->add(BookPeer::ISBN, 'NULLFK-%', Criteria::LIKE); $c->addAscendingOrderByColumn(BookPeer::ISBN); $matches = BookPeer::doSelectJoinAuthor($c); $this->assertEquals(2, count($matches), "Expected 2 matches back from new books; got back " . count($matches)); $this->assertNull($matches[0]->getAuthor(), "Expected first book author to be null"); $this->assertInstanceOf('Author', $matches[1]->getAuthor(), "Expected valid Author object for second book."); }
function clearCache() { BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); }
public function testRefFKGetJoin() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); PublisherPeer::clearInstancePool(); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $author = AuthorPeer::doSelectOne(new Criteria(), $con); // populate book instance pool $books = $author->getBooksJoinPublisher(null, $con); $sql = $con->getLastExecutedQuery(); $publisher = $books[0]->getPublisher($con); $this->assertEquals($sql, $con->getLastExecutedQuery(), 'refFK getter uses instance pool if possible'); }
/** * @link http://propel.phpdb.org/trac/ticket/699 */ public function testRollBack_NestedSwallow() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $driver = $con->getAttribute(PDO::ATTR_DRIVER_NAME); if ($driver == "mysql") { $this->markTestSkipped(); } $con->beginTransaction(); try { $a = new Author(); $a->setFirstName('Test'); $a->setLastName('User'); $a->save($con); $authorId = $a->getId(); $this->assertTrue($authorId !== null, "Expected valid new author ID"); $con->beginTransaction(); try { $con->exec('INVALID SQL'); $this->fail("Expected exception on invalid SQL"); } catch (Exception $x) { $con->rollBack(); // NO RETHROW } $a2 = new Author(); $a2->setFirstName('Test2'); $a2->setLastName('User2'); $authorId2 = $a2->save($con); $con->commit(); // this should not do anything! } catch (Exception $x) { $this->fail("No outside rollback expected."); } AuthorPeer::clearInstancePool(); $at = AuthorPeer::retrieveByPK($authorId); $this->assertNull($at, "Expected no author result for rolled-back save."); $at2 = AuthorPeer::retrieveByPK($authorId2); $this->assertNull($at2, "Expected no author2 result for rolled-back save."); }
public function testPopulateRelationOneToManyWithEmptyCollection() { $author = new Author(); $author->setLastName('I who never wrote'); $author->save($this->con); AuthorPeer::clearInstancePool(); BookPeer::clearInstancePool(); $coll = new PropelObjectCollection(); $coll->setFormatter(new PropelObjectFormatter(new ModelCriteria(null, 'Author'))); $coll[] = $author; $books = $coll->populateRelation('Book', null, $this->con); $this->assertEquals(0, $books->count()); $count = $this->con->getQueryCount(); $this->assertEquals(0, $author->countBooks()); $this->assertEquals($count, $this->con->getQueryCount()); }
/** * Method perform a DELETE on the database, given a Author or Criteria object OR a primary key value. * * @param mixed $values Criteria or Author object or primary key or array of primary keys * which is used to create the DELETE statement * @param PropelPDO $con the connection to use * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows * if supported by native driver or if emulated using Propel. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doDelete($values, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(AuthorPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } if ($values instanceof Criteria) { // invalidate the cache for all objects of this type, since we have no // way of knowing (without running a query) what objects should be invalidated // from the cache based on this Criteria. AuthorPeer::clearInstancePool(); // rename for clarity $criteria = clone $values; } elseif ($values instanceof Author) { // it's a model object // invalidate the cache for this single object AuthorPeer::removeInstanceFromPool($values); // create criteria based on pk values $criteria = $values->buildPkeyCriteria(); } else { // it's a primary key, or an array of pks $criteria = new Criteria(self::DATABASE_NAME); $criteria->add(AuthorPeer::ID, (array) $values, Criteria::IN); // invalidate the cache for this object(s) foreach ((array) $values as $singleval) { AuthorPeer::removeInstanceFromPool($singleval); } } // Set the correct dbName $criteria->setDbName(self::DATABASE_NAME); $affectedRows = 0; // initialize var to track total num of affected rows try { // use transaction because $criteria could contain info // for more than one table or we could emulating ON DELETE CASCADE, etc. $con->beginTransaction(); $affectedRows += BasePeer::doDelete($criteria, $con); AuthorPeer::clearRelatedInstancePool(); $con->commit(); return $affectedRows; } catch (PropelException $e) { $con->rollBack(); throw $e; } }
/** * Performs a DELETE on the database, given a Author or Criteria object OR a primary key value. * * @param mixed $values Criteria or Author object or primary key or array of primary keys * which is used to create the DELETE statement * @param PropelPDO $con the connection to use * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows * if supported by native driver or if emulated using Propel. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doDelete($values, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(AuthorPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } if ($values instanceof Criteria) { // rename for clarity $criteria = clone $values; } elseif ($values instanceof Author) { // it's a model object // create criteria based on pk values $criteria = $values->buildPkeyCriteria(); } else { // it's a primary key, or an array of pks $criteria = new Criteria(AuthorPeer::DATABASE_NAME); $criteria->add(AuthorPeer::ID, (array) $values, Criteria::IN); } // Set the correct dbName $criteria->setDbName(AuthorPeer::DATABASE_NAME); $affectedRows = 0; // initialize var to track total num of affected rows try { // use transaction because $criteria could contain info // for more than one table or we could emulating ON DELETE CASCADE, etc. $con->beginTransaction(); // cloning the Criteria in case it's modified by doSelect() or doSelectStmt() $c = clone $criteria; AuthorPeer::doOnDeleteSetNull($c, $con); // Because this db requires some delete cascade/set null emulation, we have to // clear the cached instance *after* the emulation has happened (since // instances get re-added by the select statement contained therein). if ($values instanceof Criteria) { AuthorPeer::clearInstancePool(); } elseif ($values instanceof Author) { // it's a model object AuthorPeer::removeInstanceFromPool($values); } else { // it's a primary key, or an array of pks foreach ((array) $values as $singleval) { AuthorPeer::removeInstanceFromPool($singleval); } } $affectedRows += BasePeer::doDelete($criteria, $con); AuthorPeer::clearRelatedInstancePool(); $con->commit(); return $affectedRows; } catch (Exception $e) { $con->rollBack(); throw $e; } }
public function testFindOneWithLeftJoinWithOneToManyAndNullObjectsAndWithAdditionalJoins() { BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); BookOpinionPeer::clearInstancePool(); BookReaderPeer::clearInstancePool(); $freud = new Author(); $freud->setFirstName("Sigmund"); $freud->setLastName("Freud"); $freud->save($this->con); $publisher = new Publisher(); $publisher->setName('Psycho Books'); $publisher->save(); $book = new Book(); $book->setAuthor($freud); $book->setTitle('Weirdness'); $book->setIsbn('abc123456'); $book->setPrice('14.99'); $book->setPublisher($publisher); $book->save(); $query = BookQuery::create()->filterByTitle('Weirdness')->innerJoinAuthor()->useBookOpinionQuery(null, Criteria::LEFT_JOIN)->leftJoinBookReader()->endUse()->with('Author')->with('BookOpinion')->with('BookReader'); $books = $query->findOne($this->con); $this->assertEquals(0, count($books->getBookOpinions())); }
public function testPopulateRelationManyToOne() { $con = Propel::getConnection(); AuthorPeer::clearInstancePool(); BookPeer::clearInstancePool(); $books = BookQuery::create()->find($con); $count = $con->getQueryCount(); $books->populateRelation('Author', null, $con); foreach ($books as $book) { $author = $book->getAuthor(); } $this->assertEquals($count + 1, $con->getQueryCount(), 'populateRelation() populates a many-to-one relationship with a single supplementary query'); }
public function testToArrayIncludeForeignObjects() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); PublisherPeer::clearInstancePool(); $c = new Criteria(); $c->add(BookPeer::TITLE, 'Don Juan'); $books = BookPeer::doSelectJoinAuthor($c); $book = $books[0]; $arr1 = $book->toArray(BasePeer::TYPE_PHPNAME, null, true); $expectedKeys = array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId', 'Author'); $this->assertEquals($expectedKeys, array_keys($arr1), 'toArray() can return sub arrays for hydrated related objects'); $this->assertEquals('George', $arr1['Author']['FirstName'], 'toArray() can return sub arrays for hydrated related objects'); $c = new Criteria(); $c->add(BookPeer::TITLE, 'Don Juan'); $books = BookPeer::doSelectJoinAll($c); $book = $books[0]; $arr2 = $book->toArray(BasePeer::TYPE_PHPNAME, null, true); $expectedKeys = array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId', 'Publisher', 'Author'); $this->assertEquals($expectedKeys, array_keys($arr2), 'toArray() can return sub arrays for hydrated related objects'); }
public function testFindPkWithOneToMany() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $book = BookQuery::create()->findOneByTitle('Harry Potter and the Order of the Phoenix', $con); $pk = $book->getPrimaryKey(); BookPeer::clearInstancePool(); $book = BookQuery::create()->setFormatter(ModelCriteria::FORMAT_ARRAY)->joinWith('Review')->findPk($pk, $con); $reviews = $book['Reviews']; $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated'); }
public static function doDelete($values, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(AuthorPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } if ($values instanceof Criteria) { AuthorPeer::clearInstancePool(); $criteria = clone $values; } elseif ($values instanceof Author) { AuthorPeer::removeInstanceFromPool($values); $criteria = $values->buildPkeyCriteria(); } else { $criteria = new Criteria(self::DATABASE_NAME); $criteria->add(AuthorPeer::ID, (array) $values, Criteria::IN); foreach ((array) $values as $singleval) { AuthorPeer::removeInstanceFromPool($singleval); } } $criteria->setDbName(self::DATABASE_NAME); $affectedRows = 0; try { $con->beginTransaction(); $affectedRows += BasePeer::doDelete($criteria, $con); $con->commit(); return $affectedRows; } catch (PropelException $e) { $con->rollBack(); throw $e; } }