/** * 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 testDelete() { $books = PropelQuery::from('Book')->setFormatter(ModelCriteria::FORMAT_ARRAY)->find(); $books->delete(); // check that the modifications are persisted BookPeer::clearInstancePool(); $books = PropelQuery::from('Book')->find(); $this->assertEquals(0, count($books)); }
public function testSerializeHydratedObject() { $book = new Book(); $book->setTitle('Foo3'); $book->setISBN('1234'); $book->save(); BookPeer::clearInstancePool(); $book = BookQuery::create()->findOneByTitle('Foo3'); $sb = serialize($book); $this->assertEquals($book, unserialize($sb)); }
public function testCountRefFk() { $book = new Book(); $book->setTitle("Test Book"); $book->setISBN("TT-EE-SS-TT"); $num = 5; for ($i = 2; $i < $num + 2; $i++) { $r = new Review(); $r->setReviewedBy('Hans ' . $num); $dt = new DateTime("now"); $dt->modify("-" . $i . " weeks"); $r->setReviewDate($dt); $r->setRecommended($i % 2 == 0); $book->addReview($r); } $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return {$num}"); $this->assertEquals($num, count($book->getReviews()), "Expected getReviews to return {$num} reviews"); $book->save(); BookPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $book = BookPeer::retrieveByPK($book->getId()); $this->assertEquals($num, $book->countReviews(), "Expected countReviews() to return {$num} (after save)"); $this->assertEquals($num, count($book->getReviews()), "Expected getReviews() to return {$num} (after save)"); // Now set different criteria and expect different results $c = new Criteria(); $c->add(ReviewPeer::RECOMMENDED, false); $this->assertEquals(floor($num / 2), $book->countReviews($c), "Expected " . floor($num / 2) . " results from countReviews(recomm=false)"); // Change Criteria, run again -- expect different. $c = new Criteria(); $c->add(ReviewPeer::RECOMMENDED, true); $this->assertEquals(ceil($num / 2), count($book->getReviews($c)), "Expected " . ceil($num / 2) . " results from getReviews(recomm=true)"); $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return {$num} with new empty Criteria"); }
public function testPruneCompositeKey() { BookstoreDataPopulator::depopulate(); BookstoreDataPopulator::populate(); // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved $c = new ModelCriteria('bookstore', 'Book'); $books = $c->find(); foreach ($books as $book) { $book->save(); } BookPeer::clearInstancePool(); $nbBookListRel = BookListRelQuery::create()->prune()->count(); $this->assertEquals(2, $nbBookListRel, 'prune() does nothing when passed a null object'); $testBookListRel = BookListRelQuery::create()->findOne(); $nbBookListRel = BookListRelQuery::create()->prune($testBookListRel)->count(); $this->assertEquals(1, $nbBookListRel, 'prune() removes an object from the result'); }
/** * 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(); }
/** * Performs a DELETE on the database, given a Book or Criteria object OR a primary key value. * * @param mixed $values Criteria or Book 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(BookPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } if ($values instanceof Criteria) { // rename for clarity $criteria = clone $values; } elseif ($values instanceof Book) { // 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(self::DATABASE_NAME); $criteria->add(BookPeer::ID, (array) $values, Criteria::IN); } // 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(); // cloning the Criteria in case it's modified by doSelect() or doSelectStmt() $c = clone $criteria; BookPeer::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) { BookPeer::clearInstancePool(); } elseif ($values instanceof Book) { // it's a model object BookPeer::removeInstanceFromPool($values); } else { // it's a primary key, or an array of pks foreach ((array) $values as $singleval) { BookPeer::removeInstanceFromPool($singleval); } } $affectedRows += BasePeer::doDelete($criteria, $con); BookPeer::clearRelatedInstancePool(); $con->commit(); return $affectedRows; } catch (PropelException $e) { $con->rollBack(); throw $e; } }
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 testSetterOneToManyWithExistingObjects() { // Ensure no data BookQuery::create()->deleteAll(); AuthorQuery::create()->deleteAll(); for ($i = 0; $i < 3; $i++) { $b = new Book(); $b->setTitle('Book ' . $i); $b->setIsbn('21234' . $i); $b->save(); } BookPeer::clearInstancePool(); $books = BookQuery::create()->find(); $a = new Author(); $a->setFirstName('Foo'); $a->setLastName('Bar'); $a->setBooks($books); $a->save(); $this->assertEquals(3, count($a->getBooks())); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(3, BookQuery::create()->count()); $i = 0; foreach ($a->getBooks() as $book) { $this->assertEquals('Book ' . $i++, $book->getTitle()); } }
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()); }
public function testSetterCollectionWithExistingObjects() { // Ensure no data BookQuery::create()->deleteAll(); BookClubListQuery::create()->deleteAll(); BookListRelQuery::create()->deleteAll(); for ($i = 0; $i < 3; $i++) { $b = new Book(); $b->setTitle('Book ' . $i); $b->save(); } BookPeer::clearInstancePool(); $books = BookQuery::create()->find(); $bookClubList = new BookClubList(); $bookClubList->setBooks($books); $bookClubList->save(); $this->assertEquals(3, count($bookClubList->getBooks())); $this->assertEquals(3, BookQuery::create()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); $this->assertEquals(3, BookListRelQuery::create()->count()); $i = 0; foreach ($bookClubList->getBooks() as $book) { $this->assertEquals('Book ' . $i++, $book->getTitle()); } }
/** * Method to invalidate the instance pool of all tables related to author * by a foreign key with ON DELETE CASCADE */ public static function clearRelatedInstancePool() { // Invalidate objects in BookPeer instance pool, // since one or more of them may be deleted by ON DELETE CASCADE/SETNULL rule. BookPeer::clearInstancePool(); }
public function testRefIsOnlySavedWhenRequired() { BookQuery::create()->deleteAll(); $book = new Book(); $book->setTitle('Propel Book'); $book->setISBN('TEST'); $book->save(); $bookId = $book->getId(); BookPeer::clearInstancePool(); $summary = $this->getMock('BookSummary'); $summary->expects($this->once())->method('isDeleted')->will($this->returnValue(false)); $summary->expects($this->once())->method('isNew')->will($this->returnValue(false)); $summary->expects($this->once())->method('isModified')->will($this->returnValue(false)); $summary->expects($this->never())->method('save'); $coll = new PropelObjectCollection(); $coll->append($summary); $book = BookQuery::create()->findOneById($bookId); // In conjunction with the mock above, this simulates loading those entries prior saving the book. $book->setBookSummarys($coll); $book->setTitle('Propel2 Book'); $book->save(); }
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 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'); }
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 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'); }
/** * Method perform a DELETE on the database, given a Book or Criteria object OR a primary key value. * * @param mixed $values Criteria or Book 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(BookPeer::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. BookPeer::clearInstancePool(); // rename for clarity $criteria = clone $values; } elseif ($values instanceof Book) { // it's a model object // invalidate the cache for this single object BookPeer::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(BookPeer::ID, (array) $values, Criteria::IN); // invalidate the cache for this object(s) foreach ((array) $values as $singleval) { BookPeer::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); BookPeer::clearRelatedInstancePool(); $con->commit(); return $affectedRows; } catch (PropelException $e) { $con->rollBack(); throw $e; } }
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'); }
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(BookPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } if ($values instanceof Criteria) { BookPeer::clearInstancePool(); $criteria = clone $values; } elseif ($values instanceof Book) { BookPeer::removeInstanceFromPool($values); $criteria = $values->buildPkeyCriteria(); } else { $criteria = new Criteria(self::DATABASE_NAME); $criteria->add(BookPeer::ID, (array) $values, Criteria::IN); foreach ((array) $values as $singleval) { BookPeer::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; } }