Пример #1
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;
 }
Пример #2
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;
    }