Example #1
0
 public static function delete_all($options = array())
 {
     $cm = \ActiveRecord\ConnectionManager::instance();
     $conn = $cm::get_connection('master');
     $table = static::table();
     $sql = new SQLBuilder($conn, $table->get_fully_qualified_table_name());
     $conditions = is_array($options) ? $options['conditions'] : $options;
     if (is_array($conditions) && !eh_um_hash($conditions)) {
         call_user_func_array(array($sql, 'delete'), $conditions);
     } else {
         $sql->delete($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();
 }
Example #2
0
 /**
  * Find records in the database.
  *
  * Finding by the primary key:
  *
  * <code>
  * # queries for the model with id=123
  * YourModel::find(123);
  *
  * # queries for model with id in(1,2,3)
  * YourModel::find(1,2,3);
  *
  * # finding by pk accepts an options array
  * YourModel::find(123,array('order' => 'name desc'));
  * </code>
  *
  * Finding by using a conditions array:
  *
  * <code>
  * YourModel::find('first', array('conditions' => array('name=?','Tito'),
  *   'order' => 'name asc'))
  * YourModel::find('all', array('conditions' => 'amount > 3.14159265'));
  * YourModel::find('all', array('conditions' => array('id in(?)', array(1,2,3))));
  * </code>
  *
  * Finding by using a hash:
  *
  * <code>
  * YourModel::find(array('name' => 'Tito', 'id' => 1));
  * YourModel::find('first',array('name' => 'Tito', 'id' => 1));
  * YourModel::find('all',array('name' => 'Tito', 'id' => 1));
  * </code>
  *
  * An options array can take the following parameters:
  *
  * <ul>
  * <li><b>select:</b> A SQL fragment for what fields to return such as: '*', 'people.*', 'first_name, last_name, id'</li>
  * <li><b>joins:</b> A SQL join fragment such as: 'JOIN roles ON(roles.user_id=user.id)' or a named association on the model</li>
  * <li><b>include:</b> TODO not implemented yet</li>
  * <li><b>conditions:</b> A SQL fragment such as: 'id=1', array('id=1'), array('name=? and id=?','Tito',1), array('name IN(?)', array('Tito','Bob')),
  * array('name' => 'Tito', 'id' => 1)</li>
  * <li><b>limit:</b> Number of records to limit the query to</li>
  * <li><b>offset:</b> The row offset to return results from for the query</li>
  * <li><b>order:</b> A SQL fragment for order such as: 'name asc', 'name asc, id desc'</li>
  * <li><b>readonly:</b> Return all the models in readonly mode</li>
  * <li><b>group:</b> A SQL group by fragment</li>
  * </ul>
  *
  * @throws {@link RecordNotFound} if no options are passed or finding by pk and no records matched
  * @return mixed An array of records found if doing a find_all otherwise a
  *   single Model object or null if it wasn't found. NULL is only return when
  *   doing a first/last find. If doing an all find and no records matched this
  *   will return an empty array.
  */
 public static function find()
 {
     $class = get_called_class();
     if (func_num_args() <= 0) {
         throw new RecordNotFound("Couldn't find {$class} without an ID");
     }
     $args = func_get_args();
     $options = static::extract_and_validate_options($args);
     $num_args = count($args);
     $single = true;
     if ($num_args > 0 && ($args[0] === 'all' || $args[0] === 'first' || $args[0] === 'last')) {
         switch ($args[0]) {
             case 'all':
                 $single = false;
                 break;
             case 'last':
                 if (!array_key_exists('order', $options)) {
                     $options['order'] = join(' DESC, ', static::table()->pk) . ' DESC';
                 } else {
                     $options['order'] = SQLBuilder::reverse_order($options['order']);
                 }
                 // fall thru
             // fall thru
             case 'first':
                 $options['limit'] = 1;
                 $options['offset'] = 0;
                 break;
         }
         $args = array_slice($args, 1);
         $num_args--;
     } elseif (1 === count($args) && 1 == $num_args) {
         $args = $args[0];
     }
     // anything left in $args is a find by pk
     if ($num_args > 0 && !isset($options['conditions'])) {
         return static::find_by_pk($args, $options);
     }
     $options['mapped_names'] = static::$alias_attribute;
     $list = static::table()->find($options);
     return $single ? !empty($list) ? $list[0] : null : $list;
 }
Example #3
0
 public function test_create_hash_from_underscored_string_with_mapped_columns()
 {
     $values = array(1, 'Tito');
     $map = array('my_name' => 'name');
     $hash = SQLBuilder::create_hash_from_underscored_string('id_and_my_name', $values, $map);
     $this->assert_equals(array('id' => 1, 'name' => 'Tito'), $hash);
 }
Example #4
0
 public function delete($data)
 {
     $data = $this->process_data($data);
     $sql = new SQLBuilder($this->conn, $this->get_fully_qualified_table_name());
     $sql->delete($data);
     $values = $sql->bind_values();
     return $this->conn->query($this->last_sql = $sql->to_s(), $values);
 }
 public function testCreateHashFromUnderscoredStringWithMappedColumns()
 {
     $values = array(1, 'Tito');
     $map = array('my_name' => 'name');
     $hash = SQLBuilder::createHashFromUnderscoredString('id_and_my_name', $values, $map);
     $this->assertEquals(array('id' => 1, 'name' => 'Tito'), $hash);
 }
 protected function createConditionsFromKeys(Model $model, $condition_keys = [], $value_keys = [])
 {
     $condition_string = \implode('_and_', $condition_keys);
     $condition_values = \array_values($model->getValuesFor($value_keys));
     // return null if all the foreign key values are null so that we don't try to do a query like "id is null"
     if (all(null, $condition_values)) {
         return null;
     }
     $conditions = SQLBuilder::createConditionsFromUnderscoredString(Table::load(\get_class($model))->conn, $condition_string, $condition_values);
     # DO NOT CHANGE THE NEXT TWO LINES. add_condition operates on a reference and will screw options array up
     if (isset($this->options['conditions'])) {
         $options_conditions = $this->options['conditions'];
     } else {
         $options_conditions = [];
     }
     return Utils::addCondition($options_conditions, $conditions);
 }
Example #7
0
 public function delete($data)
 {
     $data = $this->processData($data);
     $sql = new SQLBuilder($this->conn, $this->getFullyQualifiedTableName());
     $sql->delete($data);
     $values = $sql->bindValues();
     return $this->conn->query($this->last_sql = $sql->toString(), $values);
 }