/** * Tracks a database result * * @param mixed $model * @param mixed $result */ public function __construct($model, $result) { if ($model) { // Convert to a model $model = Jelly::class_name($model); // Instantiate the model, which we'll continually // fill with values when iterating $this->_model = new $model(); } $this->_result = $result; }
/** * Generates a builder where clause based on the provided values * * @param Jelly_Builder $builder * @param string $model Model name used in the where clause * @param mixed $values argument list used to construct the clause * @return Jelly_Builder */ protected function _where($builder, $model, array $values) { // Check for an outer and/or indexed array $where = 'where'; if (isset($values['or'])) { $where = 'or_where'; $values = is_array($values['or']) ? $values['or'] : array($values['or']); } elseif (isset($values['and'])) { $where = 'and_where'; $values = is_array($values['and']) ? $values['and'] : array($values['and']); } $count = count($values); /* One Argument is passed, can be: * interger - for the primary key lookup * string - for the name key lookup * object - a loaded model of the same class */ if ($count == 1) { if (is_integer($values[0])) { return $builder->{$where}($model . '.:primary_key', '=', $values[0]); } elseif (is_string($values[0])) { return $builder->{$where}($model . '.:name_key', '=', $values[0]); } elseif (is_object($values[0])) { $class = Jelly::class_name($model); if ($values[0] instanceof $class) { return $builder->where($model . '.:primary_key', '=', $values[0]->id()); } } } elseif ($count == 2) { if (!Jelly::meta($model)->fields($values[0])) { throw new Kohana_Exception('":field" is not a valid :model field', array(':field' => $values[0], ':model' => $model)); } return $builder->{$where}($model . '.' . $values[0], '=', $values[1]); } elseif ($count == 3) { if (!Jelly::meta($model)->fields($values[0])) { throw new Kohana_Exception('":field" is not a valid :model field', array(':field' => $values[0], ':model' => $model)); } return $builder->{$where}($model . '.' . $values[0], $values[1], $values[2]); } // Not Supported throw new Kohana_Exception('invalid or unsupported argument'); }
/** * @dataProvider providerClassNameConversion */ public function testClassNameConversion($input, $expected) { $this->assertEquals($expected, Jelly::class_name($input)); }
/** * Tests Jelly::class_name() * * @dataProvider provider_class_name */ public function test_class_name($model, $expected) { $this->assertSame($expected, Jelly::class_name($model)); }
/** * Automatically loads a model, if it exists, * into the meta table. * * Models are not required to register * themselves; it happens automatically. * * @param string $model * @return boolean */ public static function register($model) { $class = Jelly::class_name($model); $model = Jelly::model_name($model); // Don't re-initialize! if (isset(Jelly::$_models[$model])) { return TRUE; } // Can we find the class? if (class_exists($class)) { // Prevent accidentally trying to load ORM or Sprig models if (!is_subclass_of($class, "Jelly_Model")) { return FALSE; } } else { return FALSE; } // Load it into the registry Jelly::$_models[$model] = $meta = new Jelly_Meta($model); // Let the intialize() method override defaults. call_user_func(array($class, 'initialize'), $meta); // Finalize the changes $meta->finalize($model); return TRUE; }
/** * Returns results as objects * * @param string|bool $class TRUE for StdClass * @param array|null $params * @return Kohana_Database_Query */ public function as_object($class = TRUE, array $params = NULL) { // Class is TRUE, default to the model if ($class === TRUE and $this->_meta) { $class = Jelly::class_name($this->_meta->model()); } return parent::as_object($class); }
/** * Getter / setter for individual fields. * * @param string $name name of the field * @param mixed $field the field alias or object * @return Jelly_Field|Jelly_Meta|null */ public function field($name, $field = NULL) { if ($field === NULL) { // Get the field if (!isset($this->_field_cache[$name])) { // Set the resolved name to the given name for now $resolved_name = $name; if (isset($this->_aliases[$name])) { // If the field is among the aliases set the alias as resolved name $resolved_name = $this->_aliases[$name]; } if (isset($this->_fields[$resolved_name])) { // Get the field from cache using the resolved name $this->_field_cache[$name] = $this->_fields[$resolved_name]; } else { // No such field found return NULL; } } // Return the field return $this->_field_cache[$name]; } if ($this->_initialized) { // Cannot set after initialization throw new Kohana_Exception(':class already initialized, cannot set :field', array(':class' => Jelly::class_name($this->_model), ':field' => $name)); } // Set the field $this->_fields[$name] = $field; // Return Jelly_Meta return $this; }