public function testFindOneWithUsingInstancePool()
 {
     BookstoreDataPopulator::populate();
     // instance pool contains all objects by default, since they were just populated
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book');
     $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND);
     $c->orderBy('Propel\\Tests\\Bookstore\\Book.Title');
     $c->join('Propel\\Tests\\Bookstore\\Book.Author');
     $c->with('Author');
     $c->join('Propel\\Tests\\Bookstore\\Book.Publisher');
     $c->with('Publisher');
     $this->assertCorrectHydration1($c, 'with instance pool');
 }
 public function testFindOne()
 {
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->where('b.Title = ?', 'foo');
     $book = $c->findOne();
     $this->assertNull($book, 'findOne() returns null when the query returns no result');
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->orderBy('b.Title');
     $book = $c->findOne();
     $this->assertTrue($book instanceof Book, 'findOne() returns a Model object by default');
     $this->assertEquals('Don Juan', $book->getTitle(), 'find() returns the model objects matching the query');
 }
 public function testFindPksSimpleKey()
 {
     BookstoreDataPopulator::depopulate();
     BookstoreDataPopulator::populate();
     BookTableMap::clearInstancePool();
     // prepare the test data
     $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $c->orderBy('Book.Id', 'desc');
     $testBooks = $c->find();
     $testBook1 = $testBooks->pop();
     $testBook2 = $testBooks->pop();
     $q = new BookQuery();
     $books = $q->findPks(array($testBook1->getId(), $testBook2->getId()));
     $this->assertEquals(array($testBook1, $testBook2), $books->getData(), 'BaseQuery overrides findPks() to make it faster');
 }
 public function testFindOneWithoutUsingInstancePool()
 {
     BookstoreDataPopulator::populate();
     Propel::disableInstancePooling();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book');
     $c->orderBy('Propel\\Tests\\Bookstore\\Book.Title');
     $c->join('Propel\\Tests\\Bookstore\\Book.Author');
     $c->with('Author');
     $c->join('Propel\\Tests\\Bookstore\\Book.Publisher');
     $c->with('Publisher');
     $this->assertCorrectHydration1($c, 'without instance pool');
     Propel::enableInstancePooling();
 }
 public function testSelectArrayWithColumn()
 {
     BookstoreDataPopulator::depopulate($this->con);
     BookstoreDataPopulator::populate($this->con);
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book');
     $c->join('Propel\\Tests\\Bookstore\\Book.Author');
     $c->withColumn('LOWER(Propel\\Tests\\Bookstore\\Book.Title)', 'LowercaseTitle');
     $c->select(array('LowercaseTitle', 'Propel\\Tests\\Bookstore\\Book.Title'));
     $c->orderBy('Propel\\Tests\\Bookstore\\Book.Title');
     $rows = $c->find($this->con);
     $expectedSQL = $this->getSql('SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Propel\\Tests\\Bookstore\\Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC');
     $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()');
     $expectedRows = array(array('LowercaseTitle' => 'don juan', 'Propel\\Tests\\Bookstore\\Book.Title' => 'Don Juan'), array('LowercaseTitle' => 'harry potter and the order of the phoenix', 'Propel\\Tests\\Bookstore\\Book.Title' => 'Harry Potter and the Order of the Phoenix'), array('LowercaseTitle' => 'quicksilver', 'Propel\\Tests\\Bookstore\\Book.Title' => 'Quicksilver'), array('LowercaseTitle' => 'the tin drum', 'Propel\\Tests\\Bookstore\\Book.Title' => 'The Tin Drum'));
     $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()');
 }
示例#6
0
文件: Propel.php 项目: jarves/jarves
 /**
  * Maps options like limit, offset, order
  *
  * @param ModelCriteria $query
  * @param array $options
  *
  * @throws FileNotFoundException
  */
 public function mapOptions(ModelCriteria $query, $options = array())
 {
     if (isset($options['limit'])) {
         $query->limit($options['limit']);
     }
     if (isset($options['offset'])) {
         $query->offset($options['offset']);
     }
     if (isset($options['order']) && is_array($options['order'])) {
         foreach ($options['order'] as $field => $direction) {
             $fieldName = ucfirst($field);
             $tableMap = $this->tableMap;
             if (false !== ($pos = strpos($field, '.'))) {
                 $relationName = ucfirst(substr($field, 0, $pos));
                 $fieldName = ucfirst(substr($field, $pos + 1));
                 if (!($relation = $this->tableMap->getRelation($relationName))) {
                     throw new FileNotFoundException(sprintf('Relation `%s` in object `%s` not found', $relationName, $this->getObjectKey()));
                 }
                 $tableMap = $relation->getForeignTable();
             }
             if ($tableMap->hasColumnByPhpName(ucfirst($fieldName))) {
                 $column = $this->tableMap->getColumnByPhpName(ucfirst($fieldName));
                 $query->orderBy($column->getName(), $direction);
             }
         }
     }
 }