public function testFormatALotOfResults() { $nbBooks = 50; $con = Propel::getConnection(BookPeer::DATABASE_NAME); Propel::disableInstancePooling(); $book = new Book(); for ($i = 0; $i < $nbBooks; $i++) { $book->clear(); $book->setTitle('BookTest' . $i); $book->setIsbn('124' . $i); $book->save($con); } $stmt = $con->query('SELECT * FROM book'); $formatter = new PropelOnDemandFormatter(); $formatter->init(new ModelCriteria('bookstore', 'Book')); $books = $formatter->format($stmt); $this->assertTrue($books instanceof PropelOnDemandCollection, 'PropelOnDemandFormatter::format() returns a PropelOnDemandCollection'); $this->assertEquals($nbBooks, count($books), 'PropelOnDemandFormatter::format() returns a collection that counts as many rows as the results in the query'); $i = 0; foreach ($books as $book) { $this->assertTrue($book instanceof Book, 'PropelOnDemandFormatter::format() returns a collection of Model objects'); $this->assertEquals('BookTest' . $i, $book->getTitle(), 'PropelOnDemandFormatter::format() returns the model objects matching the query'); $i++; } Propel::enableInstancePooling(); }
protected function createBooks($nb = 15, $con = null) { BookQuery::create()->deleteAll($con); $books = new PropelObjectCollection(); $books->setModel('Book'); for ($i = 0; $i < $nb; $i++) { $b = new Book(); $b->setTitle('Book' . $i); $b->setIsbn('12345' . $i); $books[] = $b; } $books->save($con); }
public function testHooksCall() { AuthorQuery::create()->deleteAll(); BookQuery::create()->deleteAll(); $author = new CountableAuthor(); $author->setFirstName('Foo'); $author->setLastName('Bar'); $book = new Book(); $book->setTitle('A title'); $book->setIsbn('13456'); $author->addBook($book); $author->save(); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(1, BookQuery::create()->count()); $this->assertEquals(1, $author->nbCallPreSave); }
public function testFindOneWithEmptyLeftJoin() { // save a book with no author $b = new Book(); $b->setTitle('Foo'); $b->setIsbn('124'); $b->save(); $c = new ModelCriteria('bookstore', 'Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->where('Book.Title = ?', 'Foo'); $c->leftJoin('Book.Author'); $c->with('Author'); $c->limit(1); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $books = $c->find($con); foreach ($books as $book) { break; } $count = $con->getQueryCount(); $author = $book->getAuthor(); $this->assertNull($author, 'Related object is not hydrated if empty'); }
public function testSetterOneToMany() { // Ensure no data BookQuery::create()->deleteAll(); AuthorQuery::create()->deleteAll(); $coll = new PropelObjectCollection(); $coll->setModel('Book'); for ($i = 0; $i < 3; $i++) { $coll[] = new Book(); } $this->assertEquals(3, $coll->count()); $a = new Author(); $a->setBooks($coll); $a->save(); $this->assertInstanceOf('PropelObjectCollection', $a->getBooks()); $this->assertEquals(3, $a->getBooks()->count()); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(3, BookQuery::create()->count()); $coll->shift(); $this->assertEquals(2, $coll->count()); $a->setBooks($coll); $a->save(); $this->assertEquals(2, $a->getBooks()->count()); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(2, BookQuery::create()->count()); $newBook = new Book(); $newBook->setTitle('My New Book'); $newBook->setIsbn(1234); // Kind of new collection $coll = clone $coll; $coll[] = $newBook; $a->setBooks($coll); $a->save(); $this->assertEquals(3, $coll->count()); $this->assertEquals(3, $a->getBooks()->count()); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(3, BookQuery::create()->count()); // Add a new object $newBook1 = new Book(); $newBook1->setTitle('My New Book1'); $newBook1->setIsbn(1256); // Existing collection - The fix around reference is tested here. $coll[] = $newBook1; $a->setBooks($coll); $a->save(); $this->assertEquals(4, $coll->count()); $this->assertEquals(4, $a->getBooks()->count()); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(4, BookQuery::create()->count()); // Add the same collection $books = $a->getBooks(); $a->setBooks($books); $a->save(); $this->assertEquals(4, $books->count()); $this->assertEquals(4, $a->getBooks()->count()); $this->assertEquals(1, AuthorQuery::create()->count()); $this->assertEquals(4, BookQuery::create()->count()); }
public function testSetterCollection() { // Ensure no data BookQuery::create()->deleteAll(); BookClubListQuery::create()->deleteAll(); BookListRelQuery::create()->deleteAll(); $books = new PropelObjectCollection(); for ($i = 0; $i < 10; $i++) { $b = new Book(); $b->setTitle('My Book ' . $i); $b->setIsbn($i); $books[] = $b; } $this->assertEquals(10, $books->count()); // Basic usage $bookClubList1 = new BookClubList(); $bookClubList1->setGroupLeader('BookClubList1 Leader'); $bookClubList1->setBooks($books); $bookClubList1->save(); $this->assertEquals(10, $bookClubList1->getBooks()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); $this->assertEquals(10, BookQuery::create()->count()); $this->assertEquals(10, BookListRelQuery::create()->count()); $i = 0; foreach ($bookClubList1->getBooks() as $book) { $this->assertEquals('My Book ' . $i, $book->getTitle()); $this->assertEquals($i++, $book->getIsbn()); } // Remove an element $books->shift(); $this->assertEquals(9, $books->count()); $bookClubList1->setBooks($books); $bookClubList1->save(); $this->assertEquals(9, $bookClubList1->getBooks()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); $this->assertEquals(9, BookListRelQuery::create()->count()); $this->assertEquals(10, BookQuery::create()->count()); // Add a new object $newBook = new Book(); $newBook->setTitle('My New Book'); $newBook->setIsbn(1234); // Kind of new collection $books = clone $books; $books[] = $newBook; $bookClubList1->setBooks($books); $bookClubList1->save(); $this->assertEquals(10, $books->count()); $this->assertEquals(10, $bookClubList1->getBooks()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); $this->assertEquals(10, BookListRelQuery::create()->count()); $this->assertEquals(11, BookQuery::create()->count()); // Add a new object $newBook1 = new Book(); $newBook1->setTitle('My New Book1'); $newBook1->setIsbn(1256); // Existing collection - The fix around reference is tested here. // Ths `$books` collection has ever been setted to the `$bookClubList1` object. // Here we are adding a new object into the collection but, in this process, it // added the new object in the internal `collBooks` of the `$bookClubList1` // object too. // That's why the new object is not tracked and the cross object is not created, // in `addBook()` we consider the `collBooks` ever contains this new object. It's // not true but this is the "reference" process. // By saying "all new objects have to be added", we solve this issue. To know if // it's the best solution is the question. $books[] = $newBook1; $bookClubList1->setBooks($books); $bookClubList1->save(); $this->assertEquals(11, $books->count()); $this->assertEquals(11, $bookClubList1->getBooks()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); $this->assertEquals(11, BookListRelQuery::create()->count()); $this->assertEquals(12, BookQuery::create()->count()); // Add the same collection $books = $bookClubList1->getBooks(); $bookClubList1->setBooks($books); $bookClubList1->save(); $this->assertEquals(11, $books->count()); $this->assertEquals(11, $bookClubList1->getBooks()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); $this->assertEquals(11, BookListRelQuery::create()->count()); $this->assertEquals(12, BookQuery::create()->count()); }
public function testAddAfterRemoveKeepsReferences() { $list = new BookClubList(); $list->setGroupLeader('Archimedes Q. Porter'); $book = new Book(); $book->setTitle("Jungle Expedition Handbook"); $book->setIsbn('TEST'); $xref = new BookListRel(); $xref->setBook($book); $xref->setBookClubList($list); $xref->save(); $book->removeBookListRel($xref); $book->addBookListRel($xref); $book->save(); $this->assertCount(1, $list->getBookListRels()); $this->assertCount(1, $book->getBookListRels()); $this->assertCount(1, BookListRelPeer::doSelect(new Criteria())); $book->removeBookClubList($list); $book->addBookClubList($list); $book->save(); $this->assertCount(1, $list->getBookListRels()); $this->assertCount(1, $book->getBookListRels()); $this->assertCount(1, BookListRelPeer::doSelect(new Criteria())); }
public function testSearchMatchesSimilarObjects() { $col = new PropelObjectCollection(); $b1 = new Book(); $b1->setTitle('Bar'); $b1->setIsbn('1234'); $b1->save(); $b2 = clone $b1; $this->assertFalse($col->search($b1), 'search() returns false on an empty collection'); $col = new PropelObjectCollection(array($b1)); $this->assertTrue(0 === $col->search($b1)); $this->assertTrue(0 === $col->search($b2)); }
public function testFindOneOrCreateMakesOneQueryWhenRecordExists() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); BookQuery::create()->deleteAll($con); $book = new Book(); $book->setPrice(125); $book->setTitle('Title'); $book->setIsbn('1245'); $book->save($con); $count = $con->getQueryCount(); $book = BookQuery::create('b')->filterByPrice(125)->findOneOrCreate($con); $this->assertEquals($count + 1, $con->getQueryCount(), 'findOneOrCreate() makes only a single query when the record exists'); }
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 testFindPkDoesNotCallPreSelectWhenUsingInstancePool() { $b = new Book(); $b->setTitle('foo'); $b->setIsbn('1245'); $b->save(); $q = new mySecondBookQuery(); $this->assertFalse($q::$preSelectWasCalled); $q->findPk($b->getId()); $this->assertFalse($q::$preSelectWasCalled); }
public function testRemoveObjectOneToManyWithFkRequired() { BookSummaryQuery::create()->deleteAll(); BookQuery::create()->deleteAll(); $bookSummary = new BookSummary(); $bookSummary->setSummary('summary Propel Book'); $bookSummary2 = new BookSummary(); $bookSummary2->setSummary('summary2 Propel Book'); $book = new Book(); $book->setTitle('Propel Book'); $book->setIsbn('1235'); $book->addBookSummary($bookSummary); $book->addBookSummary($bookSummary2); $this->assertCount(2, $book->getBookSummarys()); $book->removeBookSummary($bookSummary); $bookSummaries = $book->getBookSummarys(); $this->assertCount(1, $bookSummaries); $this->assertEquals('summary2 Propel Book', reset($bookSummaries)->getSummary()); $book->save(); $bookSummary2->save(); $this->assertEquals(1, BookQuery::create()->count(), 'One Book'); $this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Summary'); $this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count()); $book->addBookSummary($bookSummary); $bookSummary->save(); $book->save(); $this->assertEquals(2, BookSummaryQuery::create()->filterBySummarizedBook($book)->count()); $book->removeBookSummary($bookSummary2); $book->save(); $this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count()); $this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Book summary because FK is required so book summary is deleted when book is saved'); }
function getBookById($Bood_Id) { $book = new Book(); $this->connect(); $result = $this->conn->query("CALL sp_search_book_by_book_id('{$Bood_Id}')"); $obj = $result->fetch_object(); $book->setBookId($obj->Book_id); $book->setTitle($obj->Title); $book->setPublisherId($obj->Publisher_id); $book->setIsbn($obj->Isbn); $book->setCategoryId($obj->Category_id); $book->setPublisherName($obj->Publisher_name); $book->setCategoryName($obj->Subject); $result->close(); // for fetch_object() //$result->free_result(); // for fetch_assoc() $this->close(); return $book; }
public function testFindOneWithOneToManyCustomOrder() { $author1 = new Author(); $author1->setFirstName('AA'); $author1->setLastName('AA'); $author2 = new Author(); $author2->setFirstName('BB'); $author2->setLastName('BB'); $book1 = new Book(); $book1->setTitle('Aaa'); $book1->setIsbn('124'); $book1->setAuthor($author1); $book1->save(); $book2 = new Book(); $book2->setTitle('Bbb'); $book2->setIsbn('35T'); $book2->setAuthor($author2); $book2->save(); $book3 = new Book(); $book3->setTitle('Ccc'); $book3->setIsbn('1256'); $book3->setAuthor($author1); $book3->save(); $authors = AuthorQuery::create()->setFormatter(ModelCriteria::FORMAT_ARRAY)->leftJoin('Author.Book')->orderBy('Book.Title')->with('Book')->find(); $this->assertEquals(2, count($authors), 'with() used on a many-to-many doesn\'t change the main object count'); }
public function testSavingParentSavesRelatedObjectsIncludingNew() { $author = AuthorPeer::retrieveByPK($this->author->getId()); // add new object before fetching old books $b3 = new Book(); $b3->setIsbn('124'); $b3->setTitle('Title'); $author->addBook($b3); $c = new Criteria(); $c->add(BookPeer::ID, $this->books[0]->getId()); $books = $author->getBooks($c); $books[0]->setTitle('Update to a book'); $author->save(); $this->assertEquals(3, $author->countBooks()); $this->assertFalse($b3->isModified()); $this->assertFalse($books[0]->isModified()); }
public function testCachePoisoning() { /* Like testAddNewObjectAfterSaveWithPoisonedCache, emphasizing cache poisoning. */ $a = new Author(); $a->setFirstName("Douglas"); $a->setLastName("Adams"); $a->save(); $c = new Criteria(); $c->add(BookPeer::TITLE, "%Restaurant%", Criteria::LIKE); $this->assertEquals(0, count($a->getBooks($c))); $b1 = new Book(); $b1->setTitle("The Hitchhikers Guide To The Galaxy"); $b1->setIsbn('12435'); $a->addBook($b1); /* Like testAddNewObjectAfterSaveWithPoisonedCache, but this time with a real criteria. */ $this->assertEquals(0, count($a->getBooks($c))); $a->save(); $this->assertFalse($b1->isNew()); $this->assertEquals(0, count($a->getBooks($c))); }