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 AuthorTableMap::clearInstancePool(); }
public function testNestedTransactionForceRollBack() { $con = Propel::getServiceContainer()->getConnection(BookTableMap::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'); AuthorTableMap::clearInstancePool(); $at = AuthorQuery::create()->findPk($authorId); $this->assertNull($at, "Rolled back transaction is not persisted in database"); $at2 = AuthorQuery::create()->findPk($authorId2); $this->assertNull($at2, "Forced Rolled back nested transaction is not persisted in database"); }
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 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 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 testRefFKGetJoin() { BookstoreDataPopulator::populate(); BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); PublisherTableMap::clearInstancePool(); $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $author = AuthorQuery::create()->findOne($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'); }
/** * * @group mysql * @group pgsql */ public function testQueryJoins() { if ($this->runningOnSQLite()) { $this->markTestSkipped('SQLite does not support right joins'); } $author = new Author(); $author->setFirstName('Steve'); $author->setLastName('Bla'); $author->save(); $author2 = new Author(); $author2->setFirstName('Blumen'); $author2->setLastName('Hosen'); $author2->save(); $book = new Book(); $book->setTitle('Book 1'); $book->setISBN('12313'); $book->save(); $log = new PolymorphicRelationLog(); $log->setMessage('author added'); $log->setAuthor($author); $log->save(); $log = new PolymorphicRelationLog(); $log->setMessage('author added'); $log->setAuthor($author2); $log->save(); $log = new PolymorphicRelationLog(); $log->setMessage('author changed'); $log->setAuthor($author); $log->save(); $log = new PolymorphicRelationLog(); $log->setMessage('book added 1'); $log->setBook($book); $log->save(); $this->assertEquals(4, PolymorphicRelationLogQuery::create()->count()); $logs = PolymorphicRelationLogQuery::create()->rightJoinAuthor()->with('Author')->orderById()->find(); $this->assertCount(3, $logs); $this->assertEquals($author, $logs[0]->getAuthor()); $this->assertEquals($author2, $logs[1]->getAuthor()); $this->assertEquals($author, $logs[2]->getAuthor()); $this->assertNull($logs[0]->getBook()); $this->assertNull($logs[1]->getBook()); $logs = PolymorphicRelationLogQuery::create()->rightJoinBook()->with('Book')->find(); $this->assertCount(1, $logs); $this->assertEquals($book, $logs[0]->getBook()); $this->assertNull($logs[0]->getAuthor()); $logs = PolymorphicRelationLogQuery::create()->useAuthorQuery(null, Criteria::RIGHT_JOIN)->filterByFirstName('Steve')->endUse()->with('Author')->find(); $this->assertCount(2, $logs); $logs = PolymorphicRelationLogQuery::create()->useAuthorQuery(null, Criteria::RIGHT_JOIN)->filterByFirstName('Blumen')->endUse()->with('Author')->find(); $this->assertCount(1, $logs); $this->assertEquals(2, PolymorphicRelationLogQuery::create()->filterByTargetId($author->getId())->filterByTargetType('author')->count()); $this->assertEquals(4, PolymorphicRelationLogQuery::create()->count()); AuthorTableMap::clearInstancePool(); $author3 = AuthorQuery::create()->leftJoinPolymorphicRelationLog()->with('PolymorphicRelationLog')->filterById($author->getId())->find()->get(0); $this->assertCount(2, $author3->getPolymorphicRelationLogs()); }