Ejemplo n.º 1
0
 /**
  * Returns an array of items for a page.
  *
  * @param  integer $offset Page offset
  * @param  integer $itemCountPerPage Number of items per page
  * @return array
  */
 public function getItems($offset, $itemCountPerPage)
 {
     $this->_select->limit($itemCountPerPage, $offset);
     return $this->_select->query()->fetchAll();
 }
Ejemplo n.º 2
0
 /**
  * _authenticateQuerySelect() - This method accepts a Zend_Db_Select object and
  * performs a query against the database with that object.
  *
  * @param  Zend_Db_Select $dbSelect
  * @throws \Zend\Authentication\Adapter\Exception - when an invalid select
  *                                       object is encountered
  * @return array
  */
 protected function _authenticateQuerySelect(DBSelect $dbSelect)
 {
     try {
         if ($this->_zendDb->getFetchMode() != Db::FETCH_ASSOC) {
             $origDbFetchMode = $this->_zendDb->getFetchMode();
             $this->_zendDb->setFetchMode(Db::FETCH_ASSOC);
         }
         $resultIdentities = $this->_zendDb->fetchAll($dbSelect->__toString());
         if (isset($origDbFetchMode)) {
             $this->_zendDb->setFetchMode($origDbFetchMode);
             unset($origDbFetchMode);
         }
     } catch (\Exception $e) {
         throw new Exception\RuntimeException('The supplied parameters to Zend\\Authentication\\Adapter\\DbTable failed to ' . 'produce a valid sql statement, please check table and column names ' . 'for validity.', 0, $e);
     }
     return $resultIdentities;
 }
Ejemplo n.º 3
0
 /**
  * Performs a validation on the select query before passing back to the parent class.
  * Ensures that only columns from the primary Zend_Db_Table are returned in the result.
  *
  * @return string|null This object as a SELECT string (or null if a string cannot be produced)
  */
 public function assemble()
 {
     $fields = $this->getPart(self::COLUMNS);
     $primary = $this->_info[AbstractTable::NAME];
     $schema = $this->_info[AbstractTable::SCHEMA];
     if (count($this->_parts[self::UNION]) == 0) {
         // If no fields are specified we assume all fields from primary table
         if (!count($fields)) {
             $this->from($primary, self::SQL_WILDCARD, $schema);
             $fields = $this->getPart(self::COLUMNS);
         }
         $from = $this->getPart(Select::FROM);
         if ($this->_integrityCheck !== false) {
             foreach ($fields as $columnEntry) {
                 list($table, $column) = $columnEntry;
                 // Check each column to ensure it only references the primary table
                 if ($column) {
                     if (!isset($from[$table]) || $from[$table]['tableName'] != $primary) {
                         throw new SelectException('Select query cannot join with another table');
                     }
                 }
             }
         }
     }
     return parent::assemble();
 }
Ejemplo n.º 4
0
 /**
  * Gets the select object to be used by the validator.
  * If no select object was supplied to the constructor,
  * then it will auto-generate one from the given table,
  * schema, field, and adapter options.
  *
  * @return DBSelect The Select object which will be used
  */
 public function getSelect()
 {
     if (null === $this->_select) {
         $db = $this->getAdapter();
         /**
          * Build select object
          */
         $select = new DBSelect($db);
         $select->from($this->_table, array($this->_field), $this->_schema);
         // Support both named and positional parameters
         if ($db->supportsParameters('named')) {
             $select->where($db->quoteIdentifier($this->_field, true) . ' = :value');
         } else {
             $select->where($db->quoteIdentifier($this->_field, true) . ' = ?');
         }
         if ($this->_exclude !== null) {
             if (is_array($this->_exclude)) {
                 $select->where($db->quoteIdentifier($this->_exclude['field'], true) . ' != ?', $this->_exclude['value']);
             } else {
                 $select->where($this->_exclude);
             }
         }
         $select->limit(1);
         $this->_select = $select;
     }
     return $this->_select;
 }
Ejemplo n.º 5
0
    /**
     * Special handling for Pdo query().
     * All bind parameter names must begin with ':'
     *
     * @param string|\Zend\Db\Select $sql The SQL statement with placeholders.
     * @param array $bind An array of data to bind to the placeholders.
     * @return \Zend\Db\Statement\Pdo
     * @throws \Zend\Db\Adapter\Exception To re-throw PdoException.
     */
    public function query($sql, $bind = array())
    {
        if (empty($bind) && $sql instanceof Select) {
            $bind = $sql->getBind();
        }

        if (is_array($bind)) {
            foreach ($bind as $name => $value) {
                if (!is_int($name) && !preg_match('/^:/', $name)) {
                    $newName = ":$name";
                    unset($bind[$name]);
                    $bind[$newName] = $value;
                }
            }
        }

        try {
            return parent::query($sql, $bind);
        } catch (\PDOException $e) {
            throw new \Zend\Db\Statement\Exception($e->getMessage(), $e->getCode(), $e);
        }
    }
Ejemplo n.º 6
0
    /**
     * Run query and returns matches, or null if no matches are found.
     *
     * @param  String $value
     * @return Array when matches are found.
     */
    protected function _query($value)
    {
        /**
         * Check for an adapter being defined. if not, fetch the default adapter.
         */
        if ($this->_adapter === null) {
            $this->_adapter = AbstractTable::getDefaultAdapter();
            if (null === $this->_adapter) {
                throw new Exception\RuntimeException('No database adapter present');
            }
        }

        /**
         * Build select object
         */
        $select = new DBSelect($this->_adapter);
        $select->from($this->_table, array($this->_field), $this->_schema)
               ->where($this->_adapter->quoteIdentifier($this->_field, true).' = ?', $value);
        if ($this->_exclude !== null) {
            if (is_array($this->_exclude)) {
                $select->where($this->_adapter->quoteIdentifier($this->_exclude['field'], true).' != ?', $this->_exclude['value']);
            } else {
                $select->where($this->_exclude);
            }
        }
        $select->limit(1);

        /**
         * Run query
         */
        $result = $this->_adapter->fetchRow($select, array(), Db::FETCH_ASSOC);

        return $result;
    }