Ejemplo n.º 1
0
 /**
  * Query. The interface is identical to Ornament\Adapter::query, except that
  * it wraps $parameters and $options in the corresponding Where and Options
  * classes for extended functionality.
  *
  * @param object $object The model to query into.
  * @param array $parameters Array of `WHERE` parameters.
  * @param array $options Hash of `OPTIONS` options.
  * @param array $ctor Optional constructor arguments.
  * @return array|false An array of initialized models of the same type as
  *  $object, or false on failure.
  */
 public function query($object, array $parameters, array $options = [], array $ctor = [])
 {
     $identifier = $this->identifier;
     $fields = $this->fields;
     foreach ($fields as &$field) {
         $field = "{$identifier}.{$field}";
     }
     $params = [];
     $this->injectBaseName($object, $parameters, $params);
     $identifier .= $this->generateJoin($fields);
     $query = new Select($this->adapter, $identifier, $fields, new Where($params), new Options($options));
     $stmt = $this->getStatement($query->__toString());
     try {
         $values = array_merge($this->parameters, $query->getBindings());
         $stmt->execute($values);
         $stmt->setFetchMode(PDO::FETCH_CLASS, get_class($object), $ctor);
         return $stmt->fetchAll();
     } catch (PDOException $e) {
         return false;
     }
 }
Ejemplo n.º 2
0
 /**
  * Retrieve a single row as an object.
  *
  * @param mixed $class Classname, object or null (defaults to StdClass) to
  *                     select into.
  * @param string $table The table(s) to query.
  * @param string $field The field (column) to query.
  * @param array $where An SQL where-array.
  * @param array $options Array of options.
  * @return mixed An object of the desired class initialized with the row's
  *  values.
  * @throws Dabble\Query\SelectException when no row was found.
  * @throws Dabble\Query\SqlException on error.
  */
 public function fetchObject($class = null, $table, $fields, $where = null, $options = [])
 {
     if (is_null($class)) {
         $class = 'StdClass';
     } elseif (is_object($class)) {
         $class = get_class($class);
     }
     if (is_scalar($fields)) {
         $fields = explode(',', $fields);
     }
     $query = new Select($this, $table, $fields, new Where($where), new Options($options));
     $stmt = $this->prepare($query->__toString());
     $stmt->execute($query->getBindings());
     $result = $stmt->fetchObject($class);
     if (!$result) {
         throw new SelectException($stmt->queryString);
     }
     return $result;
 }