Esempio n. 1
1
 public function testRefFKGetJoin()
 {
     BookstoreDataPopulator::populate();
     BookPeer::clearInstancePool();
     AuthorPeer::clearInstancePool();
     PublisherPeer::clearInstancePool();
     $con = Propel::getServiceContainer()->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 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');
 }
Esempio n. 3
0
 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());
 }
Esempio n. 4
0
 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');
 }
Esempio n. 5
0
 public function testNestedTransactionForceRollBack()
 {
     $con = Propel::getServiceContainer()->getConnection(BookPeer::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');
     AuthorPeer::clearInstancePool();
     $at = AuthorPeer::retrieveByPK($authorId);
     $this->assertNull($at, "Rolled back transaction is not persisted in database");
     $at2 = AuthorPeer::retrieveByPK($authorId2);
     $this->assertNull($at2, "Forced Rolled back nested transaction is not persisted in database");
 }
Esempio n. 6
0
 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');
 }