protected function setUp()
 {
     parent::setUp();
     BookstoreDataPopulator::populate($this->con);
     Propel::disableInstancePooling();
     $this->books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find();
 }
Example #2
0
 public function testQuery()
 {
     BookstoreDataPopulator::depopulate();
     BookstoreDataPopulator::populate();
     $book = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book b')->where('b.Title like ?', 'Don%')->orderBy('b.ISBN', 'desc')->findOne();
     $this->assertTrue($book instanceof Book);
     $this->assertEquals('Don Juan', $book->getTitle());
 }
Example #3
0
 public function testInstancePoolingReenabled()
 {
     Propel::enableInstancePooling();
     $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find($this->con);
     foreach ($books as $book) {
     }
     $this->assertTrue(Propel::isInstancePoolingEnabled());
     Propel::disableInstancePooling();
     $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ON_DEMAND)->find($this->con);
     foreach ($books as $book) {
     }
     $this->assertFalse(Propel::isInstancePoolingEnabled());
     Propel::enableInstancePooling();
 }
Example #4
0
 public function testToArray()
 {
     $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->setFormatter(ModelCriteria::FORMAT_ARRAY)->find();
     $booksArray = $books->toArray();
     $this->assertEquals(4, count($booksArray));
     $bookObjects = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->find();
     foreach ($booksArray as $key => $book) {
         $this->assertEquals($bookObjects[$key]->toArray(), $book);
     }
     $booksArray = $books->toArray();
     $keys = array(0, 1, 2, 3);
     $this->assertEquals($keys, array_keys($booksArray));
     $booksArray = $books->toArray(null, true);
     $keys = array('Propel\\Tests\\Bookstore\\Book_0', 'Propel\\Tests\\Bookstore\\Book_1', 'Propel\\Tests\\Bookstore\\Book_2', 'Propel\\Tests\\Bookstore\\Book_3');
     $this->assertEquals($keys, array_keys($booksArray));
     $booksArray = $books->toArray('Title');
     $keys = array('Harry Potter and the Order of the Phoenix', 'Quicksilver', 'Don Juan', 'The Tin Drum');
     $this->assertEquals($keys, array_keys($booksArray));
     $booksArray = $books->toArray('Title', true);
     $keys = array('Propel\\Tests\\Bookstore\\Book_Harry Potter and the Order of the Phoenix', 'Propel\\Tests\\Bookstore\\Book_Quicksilver', 'Propel\\Tests\\Bookstore\\Book_Don Juan', 'Propel\\Tests\\Bookstore\\Book_The Tin Drum');
     $this->assertEquals($keys, array_keys($booksArray));
 }
 public function testToKeyValue()
 {
     $books = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->find();
     $expected = array();
     foreach ($books as $book) {
         $expected[$book->getTitle()] = $book->getISBN();
     }
     $booksArray = $books->toKeyValue('Title', 'ISBN');
     $this->assertEquals(4, count($booksArray));
     $this->assertEquals($expected, $booksArray, 'toKeyValue() turns the collection to an associative array');
     $expected = array();
     foreach ($books as $book) {
         $expected[$book->getISBN()] = $book->getTitle();
     }
     $booksArray = $books->toKeyValue('ISBN');
     $this->assertEquals($expected, $booksArray, 'toKeyValue() uses __toString() for the value if no second field name is passed');
     $expected = array();
     foreach ($books as $book) {
         $expected[$book->getId()] = $book->getTitle();
     }
     $booksArray = $books->toKeyValue();
     $this->assertEquals($expected, $booksArray, 'toKeyValue() uses primary key for the key and __toString() for the value if no field name is passed');
 }
 /**
  * Makes an additional query to populate the objects related to the collection objects
  * by a certain relation
  *
  * @param     string     $relation  Relation name (e.g. 'Book')
  * @param     Criteria   $criteria  Optional Criteria object to filter the related object collection
  * @param     PropelPDO  $con       Optional connection object
  *
  * @return    PropelObjectCollection  The list of related objects
  */
 public function populateRelation($relation, $criteria = null, $con = null)
 {
     if (!Propel::isInstancePoolingEnabled()) {
         throw new PropelException('populateRelation() needs instance pooling to be enabled prior to populating the collection');
     }
     $relationMap = $this->getFormatter()->getTableMap()->getRelation($relation);
     if ($this->isEmpty()) {
         // save a useless query and return an empty collection
         $coll = new PropelObjectCollection();
         $coll->setModel($relationMap->getRightTable()->getClassname());
         return $coll;
     }
     $symRelationMap = $relationMap->getSymmetricalRelation();
     $query = PropelQuery::from($relationMap->getRightTable()->getClassname());
     if (null !== $criteria) {
         $query->mergeWith($criteria);
     }
     // query the db for the related objects
     $filterMethod = 'filterBy' . $symRelationMap->getName();
     $relatedObjects = $query->{$filterMethod}($this)->find($con);
     if ($relationMap->getType() == RelationMap::ONE_TO_MANY) {
         // initialize the embedded collections of the main objects
         $relationName = $relationMap->getName();
         foreach ($this as $mainObj) {
             $mainObj->initRelation($relationName);
         }
         // associate the related objects to the main objects
         $getMethod = 'get' . $symRelationMap->getName();
         $addMethod = 'add' . $relationName;
         foreach ($relatedObjects as $object) {
             $mainObj = $object->{$getMethod}();
             // instance pool is used here to avoid a query
             $mainObj->{$addMethod}($object);
         }
     } elseif ($relationMap->getType() == RelationMap::MANY_TO_ONE) {
         // nothing to do; the instance pool will catch all calls to getRelatedObject()
         // and return the object in memory
     } else {
         throw new PropelException('populateRelation() does not support this relation type');
     }
     return $relatedObjects;
 }
Example #7
0
 /**
  * Initializes a secondary ModelCriteria object, to be later merged with the current object
  *
  * @see       ModelCriteria::endUse()
  * @param     string $relationName Relation name or alias
  * @param     string $secondCriteriaClass Classname for the ModelCriteria to be used
  *
  * @return    ModelCriteria The secondary criteria object
  */
 public function useQuery($relationName, $secondaryCriteriaClass = null)
 {
     if (!isset($this->joins[$relationName])) {
         throw new PropelException('Unknown class or alias ' . $relationName);
     }
     $className = $this->joins[$relationName]->getTableMap()->getClassname();
     if (null === $secondaryCriteriaClass) {
         $secondaryCriteria = PropelQuery::from($className);
     } else {
         $secondaryCriteria = new $secondaryCriteriaClass();
     }
     if ($className != $relationName) {
         $secondaryCriteria->setModelAlias($relationName, $relationName == $this->joins[$relationName]->getRelationMap()->getName() ? false : true);
     }
     $secondaryCriteria->setPrimaryCriteria($this, $this->joins[$relationName]);
     return $secondaryCriteria;
 }