public function testDelete() { $books = PropelQuery::from('Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ARRAY)->find(); $books->delete(); // check that the modifications are persisted BookTableMap::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(); BookTableMap::clearInstancePool(); $book = BookQuery::create()->findOneByTitle('Foo3'); $sb = serialize($book); $this->assertEquals($book, unserialize($sb)); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); ReviewTableMap::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(BookTableMap::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'); }
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(); } BookTableMap::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 testPopulateRelationOneToManyWithEmptyCollection() { $author = new Author(); $author->setFirstName('Chuck'); $author->setLastName('Norris'); $author->save($this->con); AuthorTableMap::clearInstancePool(); BookTableMap::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 testFindOneWithLeftJoinWithOneToManyAndNullObjectsAndWithAdditionalJoins() { BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); BookOpinionTableMap::clearInstancePool(); BookReaderTableMap::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->find($this->con)->get(0); $this->assertEquals(0, count($books->getBookOpinions())); }
public function testFindPkWithOneToMany() { BookstoreDataPopulator::populate(); BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); ReviewTableMap::clearInstancePool(); $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $book = BookQuery::create()->findOneByTitle('Harry Potter and the Order of the Phoenix', $con); $pk = $book->getPrimaryKey(); BookTableMap::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 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('FA404-' . $i); $b->save(); } BookTableMap::clearInstancePool(); $books = BookQuery::create()->find(); $a = new Author(); $a->setFirstName('Chuck'); $a->setLastName('Norris'); $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 testToArrayIncludesForeignObjects() { BookstoreDataPopulator::populate(); BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); PublisherTableMap::clearInstancePool(); $c = new Criteria(); $c->add(BookTableMap::COL_TITLE, 'Don Juan'); $books = BookQuery::create(null, $c)->joinWith('Author')->find(); $book = $books[0]; $arr1 = $book->toArray(TableMap::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'); }
public function testPopulateRelationManyToOne() { $con = Propel::getServiceContainer()->getReadConnection(BookTableMap::DATABASE_NAME); AuthorTableMap::clearInstancePool(); BookTableMap::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 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->setIsbn($i); $b->save(); } BookTableMap::clearInstancePool(); $books = BookQuery::create()->find(); $bookClubList = new BookClubList(); $bookClubList->setGroupLeader('fabpot'); $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 testQueryFilter() { $book = new Book(); $book->setTitle('Book 1'); $book->setISBN('12313'); $book->save(); $author = new Author(); $author->setFirstName('Steve'); $author->setLastName('Bla'); $author->save(); $bookLog = new PolymorphicRelationLog(); $bookLog->setMessage('book added'); $bookLog->setBook($book); $bookLog->save(); $authorLog = new PolymorphicRelationLog(); $authorLog->setMessage('author added'); $authorLog->setAuthor($author); $authorLog->save(); PolymorphicRelationLogTableMap::clearInstancePool(); $foundLog = PolymorphicRelationLogQuery::create()->filterByBook($book)->findOne(); $this->assertEquals($bookLog->getId(), $foundLog->getId()); $this->assertEquals('book', $foundLog->getTargetType()); $this->assertEquals($book, $foundLog->getBook()); $this->assertNull($foundLog->getAuthor()); PolymorphicRelationLogTableMap::clearInstancePool(); $foundLog = PolymorphicRelationLogQuery::create()->filterByAuthor($author)->findOne(); $this->assertEquals($authorLog->getId(), $foundLog->getId()); $this->assertEquals('author', $foundLog->getTargetType()); $this->assertEquals($author, $foundLog->getAuthor()); $this->assertNull($foundLog->getBook()); // ref methods BookTableMap::clearInstancePool(); $foundBook = BookQuery::create()->filterByPolymorphicRelationLog($bookLog)->findOne(); $this->assertEquals($book->getId(), $foundBook->getId()); BookTableMap::clearInstancePool(); $foundAuthor = AuthorQuery::create()->filterByPolymorphicRelationLog($bookLog)->findOne(); $this->assertNull($foundAuthor); $foundAuthor = AuthorQuery::create()->filterByPolymorphicRelationLog($authorLog)->findOne(); $this->assertEquals($author->getId(), $foundAuthor->getId()); }
/** * Test the basic functionality of the doSelectJoin*() methods. */ public function testDoSelectJoin() { BookTableMap::clearInstancePool(); $c = new Criteria(); $books = BookQuery::create()->doSelect($c); $obj = $books[0]; // $size = strlen(serialize($obj)); BookTableMap::clearInstancePool(); $joinBooks = BookQuery::create()->joinWith('Author')->find(); $obj2 = $joinBooks[0]; $obj2Array = $obj2->toArray(TableMap::TYPE_PHPNAME, true, [], true); // $joinSize = strlen(serialize($obj2)); $this->assertEquals(count($books), count($joinBooks), "Expected to find same number of rows in doSelectJoin*() call as doSelect() call."); // $this->assertTrue($joinSize > $size, "Expected a serialized join object to be larger than a non-join object."); $this->assertTrue(array_key_exists('Author', $obj2Array)); }