/** * Return all of the rows in the result as an array. * * @param string column for associative keys * @param string column for values * @return array */ public function as_array($key = NULL, $value = NULL) { $model = Jelly::model_name($this->_model); foreach (array('key', 'value') as $var) { // Only alias meta-aliases if (${$var} && FALSE !== strpos(${$var}, ':')) { ${$var} = Jelly::meta_alias($model, ${$var}, NULL); } } return $this->_result->as_array($key, $value); }
/** * Constructs a new Jelly_Builder instance. * * $model is not actually allowed to be NULL. It has * a default because PHP throws strict errors otherwise. * * @param string $model */ public function __construct($model = NULL, $type = NULL) { parent::__construct(); if (!$model) { throw new Kohana_Exception(get_class($this) . ' requires $model to be set in the constructor'); } // Set the model and the initial from() $this->_model = Jelly::model_name($model); $this->_register_model(); // Default to loading as arrays $this->as_object(FALSE); // Save this for building the query later on $this->_type = $type; }
/** * Constructs a new Jelly_Builder instance. * * $model is not actually allowed to be NULL. It has * a default because PHP throws strict errors otherwise. * * @throws Kohana_Exception * @param string|null $model * @param mixed|null $key */ public function __construct($model = NULL, $key = NULL) { parent::__construct(); if (!$model) { throw new Kohana_Exception(get_class($this) . ' requires $model to be set in the constructor'); } // Set the model and the initial from() $this->_model = Jelly::model_name($model); $this->_meta = Jelly::meta($this->_model); $this->_initialize(); // Default to using our key if ($key !== NULL) { $this->key($key); } }
/** * How this object reacts when converted to a string * * @return string */ public function __toString() { return 'Jelly_Model: ' . Jelly::model_name($this->_model) . ' - ' . $this->name(); }
/** * @dataProvider providerModelNameConversion */ public function testModelNameConversion($input, $expected) { $this->assertEquals($expected, Jelly::model_name($input)); }
private function get_relations(Jelly_Model $model) { $model_name = Jelly::model_name($model); $model_id = $model->id(); $belongs_to = array(); $children = array(); $fields = $model->meta()->fields(); foreach ($fields as $field) { if ($field instanceof Jelly_Field_Relationship) { // TODO: Shouldn't Field_Relationship work? But it's not inherited through... if (isset($field->ignore_for_delete) && $field->ignore_for_delete) { continue; } $related_model = $field->foreign['model']; $related_model_fields = Jelly::meta($related_model)->fields(); foreach ($related_model_fields as $related_model_field) { if ($related_model_field instanceof Field_BelongsTo && $related_model_field->foreign['model'] == $model_name) { $dependencies = Jelly::select($related_model)->where($related_model_field->name, '=', $model_id)->execute(); if ($field instanceof Field_HasManyUniquely) { $add_to_array = 'children'; $link_route = Route::get('kadmium_child_edit'); $uri_params = array('controller' => $model_name, 'child_action' => 'edit', 'action' => $related_model, 'parent_id' => $model_id); } else { $add_to_array = 'belongs_to'; $link_route = Route::get('kadmium'); $uri_params = array('controller' => $related_model, 'action' => 'edit'); } foreach ($dependencies as $dependency) { array_push(${$add_to_array}, array('model' => $related_model, 'name' => $dependency->name(), 'link' => $link_route->uri($uri_params + array('id' => $dependency->id())))); } } elseif ($related_model_field instanceof Field_ManyToMany && $related_model_field->foreign['model'] == $model_name) { $get_links = Jelly::select($related_model_field->through['model'])->select($related_model_field->through['columns'][0])->where($related_model_field->through['columns'][1], '=', $model_id)->execute(); foreach ($get_links as $link) { $related = $link->{$related_model_field->through['columns'][0]}; if (!$related instanceof Jelly_Model) { $related = Jelly::select($related_model, $related); } $belongs_to[] = array('model' => $related_model, 'name' => $related->name(), 'link' => Route::get('kadmium')->uri(array('controller' => $related_model, 'action' => 'edit', 'id' => $related->id()))); } } } } } return array($belongs_to, $children); }
/** * Tests Jelly::model_name(). * * @dataProvider provider_model_name */ public function test_model_name($model, $expected) { $this->assertSame($expected, Jelly::model_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; }
/** * Model not found or no access * * @param Jelly_Model $model * @param integer $id */ public function __construct(Jelly_Model $model, $id = 0) { parent::__construct('Model not found: :model #:id', array(':id' => $id, ':model' => Jelly::model_name($model))); }
/** * Returns a string representation of the collection. * * @return string */ public function __toString() { return get_class($this) . ': ' . Jelly::model_name($this->_model) . ' (' . $this->count() . ')'; }
/** * Return model specific route * * @param Jelly_Model $model * @param string $action * @param string $params * @param string $route Defaults to model name * @return string */ public static function model(Jelly_Model $model, $action = '', $params = null, $route = null) { return Route::get($route ? $route : Jelly::model_name($model))->uri(array('id' => self::model_id($model), 'action' => $action, 'params' => $params)); }
/** * Permission denied * * @param Jelly_Model $model * @param integer $id * @param string $permission */ public function __construct(Jelly_Model $model, $id = 0, $permission = null) { parent::__construct("Permission ':permission' denied: :model #:id", array(':id' => $id, ':model' => Jelly::model_name($model), ':permission' => $permission)); }
<?php echo Html::anchor(Route::get('kadmium_child_edit')->uri(array('controller' => Request::current()->controller, 'child_action' => 'edit', 'parent_id' => $model->id(), 'action' => $field->foreign['model'], 'id' => 0)), 'Add a new ' . Inflector::humanize(Jelly::model_name($value->current())), array('class' => 'add' . $lb_class));
foreach ($value as $child_model) { ?> <li rel="<?php echo $child_model->id(); ?> "> <?php if ($is_img_list) { $image_field = $child_model->meta()->fields($field->list_as_thumbnails); $path = count($image_field->thumbnails) ? $image_field->thumbnails[0]['path'] : $image_field->path; $link_contents = Html::image(str_replace(DOCROOT, '', $path) . $child_model->get($field->list_as_thumbnails), array('alt' => $child_model->name(), 'title' => $child_model->name())); } else { $link_contents = $child_model->name(); } echo '<span>' . $link_contents . '</span>'; echo Html::anchor(Route::get('kadmium_child_edit')->uri(array('controller' => Request::current()->controller, 'child_action' => 'edit', 'parent_id' => $model->id(), 'action' => Jelly::model_name($child_model), 'id' => $child_model->id())), 'edit', array('class' => 'edit' . $lb_class)); ?> </li> <?php } ?> </ul> <ul class="has-many-uniquely" rel="<?php echo $name; ?> "> <li> <span> <?php echo View::factory($add_link_view, array('model' => $model, 'field' => $field, 'value' => $value, 'lb_class' => $lb_class)); ?>