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(); }
/** * testFilterById * * Various test for filterById functions * Id's are autoincrement so we have to use a Select to get current ID's * */ public function testFilterById() { // find by single id $book = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book b')->where('b.Title like ?', 'Don%')->orderBy('b.ISBN', 'desc')->findOne(); $c = BookQuery::create()->filterById($book->getId()); $book2 = $c->findOne(); $this->assertTrue($book2 instanceof Book); $this->assertEquals('Don Juan', $book2->getTitle()); //find range $booksAll = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book b')->orderBy('b.ID', 'asc')->find(); $booksIn = BookQuery::create()->filterById(array($booksAll[1]->getId(), $booksAll[2]->getId()))->find(); $this->assertTrue($booksIn[0] == $booksAll[1]); $this->assertTrue($booksIn[1] == $booksAll[2]); // filter by min value with greater equal $booksIn = null; $booksIn = BookQuery::create()->filterById(array('min' => $booksAll[2]->getId()))->find(); $this->assertTrue($booksIn[1] == $booksAll[3]); // filter by max value with less equal $booksIn = null; $booksIn = BookQuery::create()->filterById(array('max' => $booksAll[1]->getId()))->find(); $this->assertTrue($booksIn[1] == $booksAll[1]); // check backwards compatibility: // SELECT FROM `book` WHERE book.id IN (:p1,:p2) // must be the same as // SELECT FROM `book` WHERE (book.id>=:p1 AND book.id<=:p2) $minMax = BookQuery::create()->filterById(array('min' => $booksAll[1]->getId(), 'max' => $booksAll[2]->getId()))->find(); $In = BookQuery::create()->filterById(array($booksAll[1]->getId(), $booksAll[2]->getId()))->find(); $this->assertTrue($minMax[0] === $In[0]); $this->assertTrue(count($minMax->getData()) === count($In->getData())); }
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(); }
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('Book_0', 'Book_1', 'Book_2', '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('Book_Harry Potter and the Order of the Phoenix', 'Book_Quicksilver', 'Book_Don Juan', 'Book_The Tin Drum'); $this->assertEquals($keys, array_keys($booksArray)); }
/** * 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 $secondaryCriteriaClass 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(); /** @var self $secondaryCriteriaClass */ 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; }
/** * 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 ConnectionInterface $con Optional connection object * * @return ObjectCollection The list of related objects */ public function populateRelation($relation, $criteria = null, $con = null) { if (!Propel::isInstancePoolingEnabled()) { throw new RuntimeException(__METHOD__ . ' 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 ObjectCollection(); $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::ONE_TO_MANY === $relationMap->getType()) { // 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::MANY_TO_ONE === $relationMap->getType()) { // nothing to do; the instance pool will catch all calls to getRelatedObject() // and return the object in memory } else { throw new UnsupportedRelationException(__METHOD__ . ' does not support this relation type'); } return $relatedObjects; }
/** * Create an instance of this object query */ private function createQuery() { return PropelQuery::from(__CLASS__); }
public function testToArrayCustomDatatype() { $bookstoreCustom = PropelQuery::from('Propel\\Tests\\Bookstore\\TableWithCustomType')->setFormatter(ModelCriteria::FORMAT_ARRAY)->find(); $asArray = $bookstoreCustom->toArray(); $this->assertEquals(count($asArray), 1); $this->assertTrue($asArray[0]['ColumnWithCustomType'] instanceof CustomDatabaseType); $this->assertEquals($asArray[0]['ColumnWithCustomType']->data, 'FooBar'); }
public function testToKeyIndex() { $books = PropelQuery::from('Propel\\Tests\\Bookstore\\Book')->find(); $expected = array(); foreach ($books as $book) { $expected[$book->getTitle()] = $book; } $booksArray = $books->toKeyIndex('Title'); $this->assertEquals(4, count($booksArray)); $this->assertEquals($expected, $booksArray, 'toKeyIndex() turns the collection to `Title` indexed array'); $this->assertEquals($booksArray, $books->toKeyIndex('title')); $expected = array(); foreach ($books as $book) { $expected[$book->getISBN()] = $book; } $this->assertEquals(4, count($booksArray)); $booksArray = $books->toKeyIndex('ISBN'); $this->assertEquals($expected, $booksArray, 'toKeyIndex() uses `ISBN` for the key'); $expected = array(); foreach ($books as $book) { $expected[$book->getId()] = $book; } $this->assertEquals(4, count($booksArray)); $booksArray = $books->toKeyIndex(); $this->assertEquals($expected, $booksArray, 'toKeyIndex() uses primary key for the key'); }
/** * Get the child object of this object * * @return mixed */ public function getChildObject() { if (!$this->hasChildObject()) { return null; } $childObjectClass = $this->getDescendantClass(); $childObject = PropelQuery::from($childObjectClass)->findPk($this->getPrimaryKey()); return $childObject->hasChildObject() ? $childObject->getChildObject() : $childObject; }
public function testGetColumnValues() { $books = PropelQuery::from('Propel\\Tests\\Bookstore\\Book')->find(); $expected = []; foreach ($books as $book) { $expected[] = $book->getTitle(); } $booksArray = $books->getColumnValues('Title'); $this->assertEquals(4, count($booksArray)); $this->assertEquals($expected, $booksArray, 'getColumnValues() turns the collection to `Title` array'); $expected = []; foreach ($books as $book) { $expected[] = $book->getISBN(); } $this->assertEquals(4, count($booksArray)); $booksArray = $books->getColumnValues('ISBN'); $this->assertEquals($expected, $booksArray, 'getColumnValues() uses `ISBN` for the key'); $expected = []; foreach ($books as $book) { $expected[] = $book->getId(); } $this->assertEquals(4, count($booksArray)); $booksArray = $books->getColumnValues(); $this->assertEquals($expected, $booksArray, 'getColumnValues() uses primary key for the key'); }