Ejemplo n.º 1
0
 public function options_to_sql($options)
 {
     $table = array_key_exists('from', $options) ? $options['from'] : $this->get_fully_qualified_table_name();
     $sql = new SQLBuilder($this->conn, $table);
     if (array_key_exists('joins', $options)) {
         $sql->joins($this->create_joins($options['joins']));
         // by default, an inner join will not fetch the fields from the joined table
         if (!array_key_exists('select', $options)) {
             $options['select'] = $this->get_fully_qualified_table_name() . '.*';
         }
     }
     if (array_key_exists('select', $options)) {
         $sql->select($options['select']);
     }
     if (array_key_exists('conditions', $options)) {
         if (!is_hash($options['conditions'])) {
             if (is_string($options['conditions'])) {
                 $options['conditions'] = array($options['conditions']);
             }
             call_user_func_array(array($sql, 'where'), $options['conditions']);
         } else {
             if (!empty($options['mapped_names'])) {
                 $options['conditions'] = $this->map_names($options['conditions'], $options['mapped_names']);
             }
             $sql->where($options['conditions']);
         }
     }
     if (array_key_exists('order', $options)) {
         $sql->order($options['order']);
     }
     if (array_key_exists('limit', $options)) {
         $sql->limit($options['limit']);
     }
     if (array_key_exists('offset', $options)) {
         $sql->offset($options['offset']);
     }
     if (array_key_exists('group', $options)) {
         $sql->group($options['group']);
     }
     if (array_key_exists('having', $options)) {
         $sql->having($options['having']);
     }
     return $sql;
 }
Ejemplo n.º 2
0
 /**
  * Updates records using set in $options
  *
  * Does not instantiate models and therefore does not invoke callbacks
  *
  * Update all using a hash:
  *
  * <code>
  * YourModel::update_all(array('set' => array('name' => "Bob")));
  * </code>
  *
  * Update all using a string:
  *
  * <code>
  * YourModel::update_all(array('set' => 'name = "Bob"'));
  * </code>
  *
  * An options array takes the following parameters:
  *
  * <ul>
  * <li><b>set:</b> String/hash of field names and their values to be updated with
  * <li><b>conditions:</b> Conditions using a string/hash/array</li>
  * <li><b>limit:</b> Limit number of records to update (MySQL & Sqlite only)</li>
  * <li><b>order:</b> A SQL fragment for ordering such as: 'name asc', 'id desc, name asc' (MySQL & Sqlite only)</li>
  * </ul>
  *
  * @params array $options
  * return integer Number of rows affected
  */
 public static function update_all($options = array())
 {
     $table = static::table();
     $conn = static::connection();
     $sql = new SQLBuilder($conn, $table->get_fully_qualified_table_name());
     $sql->update($options['set']);
     if (isset($options['conditions']) && ($conditions = $options['conditions'])) {
         if (is_array($conditions) && !is_hash($conditions)) {
             call_user_func_array(array($sql, 'where'), $conditions);
         } else {
             $sql->where($conditions);
         }
     }
     if (isset($options['limit'])) {
         $sql->limit($options['limit']);
     }
     if (isset($options['order'])) {
         $sql->order($options['order']);
     }
     $values = $sql->bind_values();
     $ret = $conn->query($table->last_sql = $sql->to_s(), $values);
     return $ret->rowCount();
 }