Пример #1
0
 /**
  * Fetches rows by primary key.  The argument specifies one or more primary
  * key value(s).  To find multiple rows by primary key, the argument must
  * be an array.
  *
  * This method accepts a variable number of arguments.  If the table has a
  * multi-column primary key, the number of arguments must be the same as
  * the number of columns in the primary key.  To find multiple rows in a
  * table with a multi-column primary key, each argument must be an array
  * with the same number of elements.
  *
  * The find() method always returns a Rowset object, even if only one row
  * was found.
  *
  * @param  mixed $key The value(s) of the primary keys.
  * @return Statement Row(s) matching the criteria.
  * @throws DataObjectException
  */
 public function find()
 {
     $args = func_get_args();
     $keyNames = array_values($this->_primary);
     if (count($args) != count($keyNames)) {
         throw new DataObjectException("Too few or too many columns for the primary key");
     }
     $whereList = [];
     $numberTerms = 0;
     foreach ($args as $keyPosition => $keyValues) {
         $keyValuesCount = count($keyValues);
         // Coerce the values to an array.
         // Don't simply typecast to array, because the values
         // might be Expr objects.
         if (!is_array($keyValues)) {
             $keyValues = array($keyValues);
         }
         if ($numberTerms == 0) {
             $numberTerms = $keyValuesCount;
         } else {
             if ($keyValuesCount != $numberTerms) {
                 throw new DataObjecgtException("Missing value(s) for the primary key");
             }
         }
         $keyValues = array_values($keyValues);
         for ($i = 0; $i < $keyValuesCount; ++$i) {
             if (!isset($whereList[$i])) {
                 $whereList[$i] = [];
             }
             $whereList[$i][$keyPosition] = $keyValues[$i];
         }
     }
     if (count($whereList) === 0) {
         // empty where clause should return empty rowset
         throw new SelectException('Where clause is empty.');
     }
     $whereOrTerms = [];
     $tableName = $this->_db->quoteTableAs($this->_name, null, true);
     foreach ($whereList as $keyValueSets) {
         $whereAndTerms = [];
         foreach ($keyValueSets as $keyPosition => $keyValue) {
             //$type = $this->_metadata[$keyNames[$keyPosition]]['DATA_TYPE'];
             $columnName = $this->_db->quoteIdentifier($keyNames[$keyPosition], true);
             $whereAndTerms[] = $this->_db->quoteInto($tableName . '.' . $columnName . ' = ?', $keyValue);
         }
         $whereOrTerms[] = '(' . implode(' AND ', $whereAndTerms) . ')';
     }
     return $this->select()->where(implode(' OR ', $whereOrTerms))->yieldAll();
 }
Пример #2
0
 /**
  * Constructs where statement for retrieving row(s).
  *
  * @param bool $useDirty
  * @return array
  */
 protected function _getWhereQuery($useDirty = true)
 {
     $where = array();
     $primaryKey = $this->_getPrimaryKey($useDirty);
     //$info = static::info();
     //$metadata = $info[self::METADATA]; FIXME 这个暂时无解
     // retrieve recently updated row using primary keys
     $where = array();
     foreach ($primaryKey as $column => $value) {
         $tableName = static::$_db->quoteIdentifier(static::$_name, true);
         //$type = $metadata[$column]['DATA_TYPE'];
         $columnName = static::$_db->quoteIdentifier($column, true);
         $where[] = static::$_db->quoteInto("{$tableName}.{$columnName} = ?", $value);
         //, $type FIXME 这个暂时无解
     }
     return $where;
 }
Пример #3
0
 /**
  * Internal function for creating the where clause
  *
  * @param string   $condition
  * @param mixed	$value  optional
  * @param string   $type   optional
  * @param boolean  $bool  true = AND, false = OR
  * @return string  clause
  */
 protected function _where($condition, $value = null, $bool = true)
 {
     if (count($this->_parts[self::UNION])) {
         throw new SelectException("Invalid use of where clause with " . self::SQL_UNION);
     }
     if ($value !== null) {
         $condition = $this->_adapter->quoteInto($condition, $value);
     }
     $cond = "";
     if ($this->_parts[self::WHERE]) {
         if ($bool === true) {
             $cond = self::SQL_AND . ' ';
         } else {
             $cond = self::SQL_OR . ' ';
         }
     }
     return $cond . "({$condition})";
 }