Exemplo n.º 1
0
 /**
  * @return void
  */
 protected function loadChunk()
 {
     $this->query->setOffset($this->offset);
     $this->query->setLimit($this->chunkSize);
     $this->currentDataSet = $this->query->find();
     $this->offset += $this->chunkSize;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function loadChoiceList($value = null)
 {
     if ($this->choiceList) {
         return $this->choiceList;
     }
     $models = iterator_to_array($this->query->find());
     $this->choiceList = $this->factory->createListFromChoices($models, $value);
     return $this->choiceList;
 }
Exemplo n.º 3
0
 /**
  * @param  ModelCriteria $criteria
  * @return $this|null
  *
  * Loads a model criteria.
  * Warning: This doesn't goodly support multi table queries.
  * If you need to use more than one table, use a PDO instance and
  * use the fetchArray() method, or select every columns you need
  */
 public function loadModelCriteria(ModelCriteria $criteria)
 {
     $propelData = $criteria->find();
     if (empty($propelData)) {
         return null;
     }
     $asColumns = $propelData->getFormatter()->getAsColumns();
     /**
      * Format it correctly
      * After this pass, we MUST have a 2D array.
      * The first may be keyed with integers.
      */
     $formattedResult = $propelData->toArray(null, false, TableMap::TYPE_COLNAME);
     if (count($asColumns) > 1) {
         /**
          * Request with multiple select
          * Apply propel aliases
          */
         $formattedResult = $this->applyAliases($formattedResult, $asColumns);
     } elseif (count($asColumns) === 1) {
         /**
          * Request with one select
          */
         $key = str_replace("\"", "", array_keys($asColumns)[0]);
         $formattedResult = [[$key => $formattedResult[0]]];
     }
     $data = $this->applyAliases($formattedResult, $this->aliases);
     /**
      * Then store it
      */
     $this->data = $data;
     return $this;
 }
 public function testPreAndPostDelete()
 {
     $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $books = $c->find();
     $count = count($books);
     $book = $books->shift();
     $this->con->lastAffectedRows = 0;
     $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', '\\Propel\\Tests\\Bookstore\\Book', 'b');
     $c->where('b.Id = ?', $book->getId());
     $nbBooks = $c->delete($this->con);
     $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after delete() even if preDelete() returns not null');
     $this->con->lastAffectedRows = 0;
     $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $nbBooks = $c->deleteAll($this->con);
     $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after deleteAll() even if preDelete() returns not null');
 }
 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 testUpdateOneByOne()
 {
     $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
     BookstoreDataPopulator::depopulate($con);
     BookstoreDataPopulator::populate($con);
     // 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();
     }
     $count = $con->getQueryCount();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book');
     $nbBooks = $c->update(array('Title' => 'foo'), $con, true);
     $this->assertEquals(4, $nbBooks, 'update() returns the number of updated rows');
     $this->assertEquals($count + 1 + 4, $con->getQueryCount(), 'update() updates the objects one by one when called with true as last parameter');
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->where('b.Title = ?', 'foo');
     $nbBooks = $c->count();
     $this->assertEquals(4, $nbBooks, 'update() updates all records by default');
     BookstoreDataPopulator::depopulate($con);
     BookstoreDataPopulator::populate($con);
     // 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();
     }
     $count = $con->getQueryCount();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->where('b.Title = ?', 'Don Juan');
     $nbBooks = $c->update(array('ISBN' => '3456'), $con, true);
     $this->assertEquals(1, $nbBooks, 'update() updates only the records matching the criteria');
     $this->assertEquals($count + 1 + 1, $con->getQueryCount(), 'update() updates the objects one by one when called with true as last parameter');
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->where('b.Title = ?', 'Don Juan');
     $book = $c->findOne();
     $this->assertEquals('3456', $book->getISBN(), 'update() updates only the records matching the criteria');
 }
 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');
 }
Exemplo n.º 8
0
 public function testFindOneWithColumnAndAlias()
 {
     BookstoreDataPopulator::populate();
     BookTableMap::clearInstancePool();
     AuthorTableMap::clearInstancePool();
     ReviewTableMap::clearInstancePool();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book');
     $c->filterByTitle('Harry Potter and the Order of the Phoenix');
     $c->joinWith('Propel\\Tests\\Bookstore\\Book.BookSummary');
     $c->joinWith('Propel\\Tests\\Bookstore\\Book.Review');
     $c->join('Propel\\Tests\\Bookstore\\Book.Author');
     $c->withColumn('Author.FirstName', 'AuthorName');
     $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
     $book = $c->find($con)->get(0);
     $count = $con->getQueryCount();
     $reviews = $book->getReviews();
     //Washington Post
     $this->assertTrue($book instanceof Book, 'withColumn() do not change the resulting model class');
     $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated');
     $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query ');
     $this->assertEquals('J.K.', $book->getVirtualColumn('AuthorName'), 'ObjectFormatter adds withColumns as virtual columns');
 }
Exemplo n.º 9
0
 /**
  * @param ModelCriteria $search
  *
  * @return ObjectCollection
  */
 protected function searchWithOffset(ModelCriteria $search)
 {
     $limit = intval($this->getArgValue('limit'));
     if ($limit >= 0) {
         $search->limit($limit);
     }
     $search->offset(intval($this->getArgValue('offset')));
     return $search->find();
 }
 public function testFormatterWithSelect()
 {
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book');
     $c->keepQuery(false);
     // just for this test's purpose
     $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND);
     $c->select(array('Id', 'Title'));
     $rows = $c->find($this->con);
     $this->assertTrue($c->getFormatter() instanceof \Propel\Runtime\Formatter\OnDemandFormatter, 'The formatter is preserved');
 }
Exemplo n.º 11
0
 public function testFindWithLeftJoinWithManyToOneAndNullObject()
 {
     if (!$this->runningOnSQLite()) {
         $this->markTestSkipped('This test is designed for SQLite as it saves an empty object.');
     }
     BookTableMap::clearInstancePool();
     AuthorTableMap::clearInstancePool();
     ReviewTableMap::clearInstancePool();
     $review = new Review();
     $review->setReviewedBy("Peter");
     $review->setRecommended(true);
     $review->save($this->con);
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Review');
     $c->setFormatter(ModelCriteria::FORMAT_ARRAY);
     $c->leftJoinWith('Propel\\Tests\\Bookstore\\Review.Book');
     $c->leftJoinWith('Book.Author');
     // should not raise a notice
     $reviews = $c->find($this->con);
     $this->assertTrue(true);
 }
Exemplo n.º 12
0
 public function testFindOneWithOneToManyThenManyToOneUsingAlias()
 {
     BookstoreDataPopulator::populate();
     BookTableMap::clearInstancePool();
     AuthorTableMap::clearInstancePool();
     ReviewTableMap::clearInstancePool();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
     $c->add(AuthorTableMap::LAST_NAME, 'Rowling');
     $c->leftJoinWith('Propel\\Tests\\Bookstore\\Author.Book b');
     $c->leftJoinWith('b.Review r');
     $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
     $authors = $c->find($con);
     $this->assertEquals(1, count($authors), 'with() does not duplicate the main object');
     $rowling = $authors[0];
     $count = $con->getQueryCount();
     $this->assertEquals($rowling->getFirstName(), 'J.K.', 'Main object is correctly hydrated');
     $books = $rowling->getBooks();
     $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query ');
     $this->assertEquals(1, count($books), 'Related objects are correctly hydrated');
     $book = $books[0];
     $this->assertEquals($book->getTitle(), 'Harry Potter and the Order of the Phoenix', 'Related object is correctly hydrated');
     $reviews = $book->getReviews();
     $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query ');
     $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated');
 }
Exemplo n.º 13
0
 public function testFindWithLeftJoinWithManyToOneAndNullObject()
 {
     if ('sqlite' !== $this->con->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
         $this->markTestSkipped('This test is designed for SQLite as it saves an empty object.');
     }
     BookTableMap::clearInstancePool();
     AuthorTableMap::clearInstancePool();
     ReviewTableMap::clearInstancePool();
     $review = new Review();
     $review->save($this->con);
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Review');
     $c->setFormatter(ModelCriteria::FORMAT_ARRAY);
     $c->leftJoinWith('Propel\\Tests\\Bookstore\\Review.Book');
     $c->leftJoinWith('Book.Author');
     // should not raise a notice
     $reviews = $c->find($this->con);
     $this->assertTrue(true);
 }