public function testDelete() { $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ARRAY)->find(); $books->delete(); // check that the modifications are persisted BookPeer::clearInstancePool(); $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\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 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()); } }
public function testToArrayIncludesForeignObjects() { 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, array(), 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, array(), 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::getServiceContainer()->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 function testPopulateRelationOneToManyWithEmptyCollection() { $author = new Author(); $author->setLastName('I who never wrote'); $author->save($this->con); AuthorPeer::clearInstancePool(); BookPeer::clearInstancePool(); $coll = new ObjectCollection(); $coll->setFormatter(new ObjectFormatter(new ModelCriteria(null, 'Propel\\Tests\\Bookstore\\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 testPruneCompositeKey() { BookstoreDataPopulator::depopulate(); BookstoreDataPopulator::populate(); // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved $c = new ModelCriteria('bookstore', 'Propel\\Tests\\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'); }
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::getServiceContainer()->getReadConnection(BookPeer::DATABASE_NAME); 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'); }
/** * 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('Propel\\Tests\\Bookstore\\Author', $matches[1]->getAuthor(), "Expected valid Author object for second book."); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->filterByTitle('The Tin Drum'); $c->join('Propel\\Tests\\Bookstore\\Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $c->limit(1); $con = Propel::getServiceContainer()->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, 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'ObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'ObjectFormatter correctly hydrates all virtual columns'); }