Ejemplo n.º 1
0
 public function assert_conditions($expected_sql, $values, $underscored_string, $map = null)
 {
     $cond = SQLBuilder::create_conditions_from_underscored_string($this->table->conn, $underscored_string, $values, $map);
     $this->assert_sql_has($expected_sql, array_shift($cond));
     if ($values) {
         $this->assert_equals(array_values(array_filter($values, function ($s) {
             return $s !== null;
         })), array_values($cond));
     } else {
         $this->assert_equals(array(), $cond);
     }
 }
Ejemplo n.º 2
0
 /**
  * Enables the use of dynamic finders.
  *
  * Dynamic finders are just an easy way to do queries quickly without having to
  * specify an options array with conditions in it.
  *
  * <code>
  * SomeModel::find_by_first_name('Tito');
  * SomeModel::find_by_first_name_and_last_name('Tito','the Grief');
  * SomeModel::find_by_first_name_or_last_name('Tito','the Grief');
  * SomeModel::find_all_by_last_name('Smith');
  * SomeModel::count_by_name('Bob')
  * SomeModel::count_by_name_or_state('Bob','VA')
  * SomeModel::count_by_name_and_state('Bob','VA')
  * </code>
  *
  * You can also create the model if the find call returned no results:
  *
  * <code>
  * Person::find_or_create_by_name('Tito');
  *
  * # would be the equivalent of
  * if (!Person::find_by_name('Tito'))
  *   Person::create(array('Tito'));
  * </code>
  *
  * Some other examples of find_or_create_by:
  *
  * <code>
  * Person::find_or_create_by_name_and_id('Tito',1);
  * Person::find_or_create_by_name_and_id(array('name' => 'Tito', 'id' => 1));
  * </code>
  *
  * @param string $method Name of method
  * @param mixed $args Method args
  * @return Model
  * @throws {@link ActiveRecordException} if invalid query
  * @see find
  */
 public static function __callStatic($method, $args)
 {
     $options = static::extract_and_validate_options($args);
     $create = false;
     if (substr($method, 0, 17) == 'find_or_create_by') {
         $attributes = substr($method, 17);
         // can't take any finders with OR in it when doing a find_or_create_by
         if (strpos($attributes, '_or_') !== false) {
             throw new ActiveRecordException("Cannot use OR'd attributes in find_or_create_by");
         }
         $create = true;
         $method = 'find_by' . substr($method, 17);
     }
     if (substr($method, 0, 7) === 'find_by') {
         $attributes = substr($method, 8);
         $options['conditions'] = SQLBuilder::create_conditions_from_underscored_string($attributes, $args, static::$alias_attribute);
         if (!($ret = static::find('first', $options)) && $create) {
             return static::create(SQLBuilder::create_hash_from_underscored_string($attributes, $args, static::$alias_attribute));
         }
         return $ret;
     } elseif (substr($method, 0, 11) === 'find_all_by') {
         $options['conditions'] = SQLBuilder::create_conditions_from_underscored_string(substr($method, 12), $args, static::$alias_attribute);
         return static::find('all', $options);
     } elseif (substr($method, 0, 8) === 'count_by') {
         $options['conditions'] = SQLBuilder::create_conditions_from_underscored_string(substr($method, 9), $args, static::$alias_attribute);
         return static::count($options);
     }
     throw new ActiveRecordException("Call to undefined method: {$method}");
 }
Ejemplo n.º 3
0
 public function test_create_conditions_from_underscored_string_with_mapped_columns()
 {
     $x = array(1, 'Tito');
     $map = array('my_name' => 'name');
     $conditions = SQLBuilder::create_conditions_from_underscored_string('id_and_my_name', $x, $map);
     $this->assert_equals(array('id=? AND name=?', 1, 'Tito'), $conditions);
 }