/** * Returns the result of the query $query as a list of objects. * * Returns the persistent objects found for $class using the submitted * $query. $query should be created using {@link createFindQuery()} to * ensure correct alias mappings and can be manipulated as needed. * * Example: * <code> * $q = $session->createFindQuery( 'Person' ); * $allPersons = $session->find( $q, 'Person' ); * </code> * * If you are retrieving large result set, consider using {@link * findIterator()} instead. * * Example: * <code> * $q = $session->createFindQuery( 'Person' ); * $objects = $session->findIterator( $q, 'Person' ); * * foreach( $objects as $object ) * { * // ... * } * </code> * * @throws ezcPersistentDefinitionNotFoundException * if there is no such persistent class. * @throws ezcPersistentQueryException * if the find query failed. * @throws ezcBaseValueException * if $query parameter is not an instance of ezcPersistentFindQuery * or ezcQuerySelect. Or if $class is missing if you use * ezcQuerySelect. * * @param ezcPersistentFindQuery|ezcQuerySelect $query * @param string $class * * @return array(object($class)) * * @apichange This method will only accept an instance of * ezcPersistentFindQuery as the $query parameter in future * major releases. The $class parameter will be removed. */ public function find($query, $class = null) { // Sanity checks if (!is_object($query) || !$query instanceof ezcPersistentFindQuery && !$query instanceof ezcQuerySelect) { throw new ezcBaseValueException('query', $query, 'ezcPersistentFindQuery (or ezcQuerySelect)'); } if ($query instanceof ezcQuerySelect && $class === null) { throw new ezcBaseValueException('class', $class, 'must be present, if ezcQuerySelect is used for $query'); } // Extract class name and select query form parameter if ($query instanceof ezcPersistentFindQuery) { $class = $query->className; $query = $query->query; } $def = $this->definitionManager->fetchDefinition($class); $rows = $this->session->performQuery($query)->fetchAll(PDO::FETCH_ASSOC); // Convert all the rows to states and then to objects. $result = array(); foreach ($rows as $row) { $object = new $def->class(); $object->setState(ezcPersistentStateTransformer::rowToStateArray($row, $def)); $result[$row[$def->idProperty->resultColumnName]] = $object; } return $result; }
/** * Returns the next persistent object in the result set. * * The next object is set to the current object of the iterator. * Returns null and sets the current object to null if there * are no more results in the result set. * * @return object */ public function next() { $row = false; try { $row = $this->stmt->fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { $this->object = null; return; } // SQLite returns empty array on faulty statement! if ($row !== false && (is_array($row) && sizeof($row) != 0) && $this->checkDef()) { if ($this->object == null) { $this->object = new $this->def->class(); } else { // Issue #14473: ezcPersistentFindIterator overwrites last object $this->object = clone $this->object; } $this->object->setState(ezcPersistentStateTransformer::rowToStateArray($row, $this->def)); } else { $this->object = null; } return $this->object; }
/** * Returns the result of the query $query as a list of objects. * * Returns the persistent objects found for $class using the submitted * $query. $query should be created using {@link createFindQuery()} to * ensure correct alias mappings and can be manipulated as needed. * * Example: * <code> * $q = $session->createFindQuery( 'Person' ); * $allPersons = $session->find( $q, 'Person' ); * </code> * * If you are retrieving large result set, consider using {@link * findIterator()} instead. * * Example: * <code> * $q = $session->createFindQuery( 'Person' ); * $objects = $session->findIterator( $q, 'Person' ); * * foreach( $objects as $object ) * { * // ... * } * </code> * * @throws ezcPersistentDefinitionNotFoundException * if there is no such persistent class. * @throws ezcPersistentQueryException * if the find query failed. * * @param ezcQuerySelect $query * @param string $class * * @return array(object($class)) */ public function find(ezcQuerySelect $query, $class) { $def = $this->definitionManager->fetchDefinition($class); $rows = $this->session->performQuery($query)->fetchAll(PDO::FETCH_ASSOC); // Convert all the rows to states and then to objects. $result = array(); foreach ($rows as $row) { $object = new $def->class(); $object->setState(ezcPersistentStateTransformer::rowToStateArray($row, $def)); $result[] = $object; } return $result; }
/** * Test case for issue #12108. */ public function testMissingReverseColumnLookup() { // Load def without def manager $def = (require dirname(__FILE__) . '/../data/persistenttestobject.php'); try { ezcPersistentStateTransformer::rowToStateArray(array(), $def); $this->fail('Exception not thrown on state transformation without proper reverse-lookup.'); } catch (ezcPersistentObjectException $e) { } }