Пример #1
0
 public function update(Dbi_Model $query, array $data)
 {
     self::$queryCount++;
     $components = $query->components();
     $update = new BuildSql_Update();
     $update->table($query->prefix() . $components['table']);
     foreach ($components['where'] as $where) {
         $orStatements = array();
         $orParameters = array();
         foreach ($where->expressions() as $or) {
             $orStatements[] = $or->statement();
             $orParameters = array_merge($orParameters, $or->parameters());
         }
         $args = array_merge(array(implode(' OR ', $orStatements)), $orParameters);
         call_user_func_array(array($update, 'where'), $args);
     }
     // Get rid of fields that are not defined in the schema.
     // TODO: Should undefined fields generate an error?
     foreach ($data as $key => $value) {
         if (is_null($query->field($key))) {
             unset($data[$key]);
         } else {
             // Convert arrays to JSON
             // (Objects depend on __toString() for conversion)
             if (is_array($value)) {
                 $data[$key] = json_encode($value);
             }
         }
     }
     $update->set($data);
     mysql_query($update->query());
     if (mysql_error()) {
         echo "{$update->query()}<br/>";
         throw new Exception(mysql_error());
     }
 }