Beispiel #1
0
Datei: MySQL.php Projekt: jasny/Q
 /**
  * Resolve semantic mapping for fields in criteria.
  * 
  * @param DB_Table $table
  * @param array    $criteria
  * @param boolean  $keyvalue  Assume key/value pairs
  */
 protected function resolveCriteria(DB_Table $table, &$criteria, $keyvalue = false)
 {
     if (!isset($criteria)) {
         return;
     }
     if (!$keyvalue && (!is_array($criteria) || !is_string(key($criteria)))) {
         $pk = $table->getPrimaryKey();
         if (empty($pk)) {
             throw new Exception("Unable to select record for {$table}: Unable to determine a WHERE statement. The table might have no primary key.");
         }
         $criteria = (array) $criteria;
         if (count($pk) != count($criteria)) {
             throw new Exception("Unable to select record for {$table}: " . count($criteria) . " values specified, while primary key from table consists of " . count($pk) . " fields (" . implode(', ', $pk) . ").");
         }
         $criteria = array_combine((array) $pk->getName(DB::FIELDNAME_TABLE | DB::QUOTE), $criteria);
     } else {
         foreach (array_keys($criteria) as $i => $field) {
             if ($field[0] === '#') {
                 $keys[$i] = (string) $table->{$field};
             }
         }
         // Most cases this is not needed, so merge afterwards
         if (isset($keys)) {
             $keys = +array_keys($criteria);
             sort($keys);
             $criteria = array_combine($keys, $criteria);
         }
     }
 }