function get_collection_uri(moojon_base_model $model) { $route = get_collection_rest_route($model); $parent_resource = ''; $pattern = $route->get_pattern(); if ($resource = moojon_uri::get_or_null('resource')) { $resource = moojon_inflect::singularize($resource); $resource_model = new $resource(); $table = $model->get_table(); $class = $model->get_class(); if ($resource_model->has_has_many_relationship($table) || $resource_model->has_has_many_to_many_relationship($table)) { $parent_resource = moojon_uri::get_uri() . '/'; } else { if ($resource == $class) { $parent_resource = moojon_uri::get_uri(); $parent_resource_id = substr($parent_resource, strrpos($parent_resource, '/') + 1); if ($parent_resource_id == $table) { $parent_resource_id = ''; } $parent_resource = substr($parent_resource, 0, strrpos($parent_resource, '/')); if ($parent_resource) { $pattern = ''; } if ($resource_model->is_belongs_to_relationship_column(moojon_primary_key::get_foreign_key($class))) { $slash = $parent_resource_id ? '/' : ''; $pattern .= "{$slash}{$parent_resource_id}/{$table}"; } } else { if ($resource_model->has_relationship($class)) { $belongs_to_relationship = $resource_model->get_relationship($class); if (get_class($belongs_to_relationship) == 'moojon_belongs_to_relationship') { $parent_resource = moojon_uri::get_uri() . '/'; } } } } } return moojon_config::get('index_file') . $parent_resource . $pattern . '/'; }
public static function get_relationship_object_where(moojon_base_relationship $relationship, moojon_base_model $accessor) { $key = $relationship->get_key(); switch (get_class($relationship)) { case 'moojon_has_one_relationship': $foreign_table = $relationship->get_foreign_table(); $foreign_key = $relationship->get_foreign_key(); $return = "`{$foreign_table}`.`{$key}` = :{$foreign_key}"; break; case 'moojon_has_many_relationship': $foreign_table = $relationship->get_foreign_table(); $foreign_key = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$foreign_table}`.`{$foreign_key}` = :{$key}"; break; case 'moojon_has_many_to_many_relationship': $foreign_table = moojon_inflect::pluralize($relationship->get_class($accessor)); $foreign_key1 = moojon_primary_key::get_foreign_key($relationship->get_foreign_table()); $foreign_key2 = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$key}` IN (SELECT `{$foreign_key1}` FROM `{$foreign_table}` WHERE `{$foreign_key2}` = :{$key})"; break; case 'moojon_belongs_to_relationship': $foreign_key = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$key}` = :{$foreign_key}"; break; } return $return; }
function relationship_tables(moojon_base_model $model, $relationships = array(), $column_names = array(), $attributes = array(), $no_records_messages = array(), $count = null) { if ($model->has_relationships()) { $relationships = $relationships ? $relationships : $model->get_relationships(); foreach ($relationships as $key => $value) { if (is_subclass_of($value, 'moojon_base_relationship')) { $relationships[$key] = $model->{$key}; } } $div = div_tag(); foreach ($relationships as $key => $value) { switch ($model->get_relationship_type($key)) { case 'moojon_has_many_relationship': case 'moojon_has_many_to_many_relationship': $div->add_child(h3_tag(title_text($key))); $relationship_model = $model->get_relationship_model($key); $div->add_child(actions_ul(array(new_member_tag($relationship_model)))); $div->add_child('<br /><br /><br />'); $child_column_names = array_key_exists($key, $column_names) ? $column_names[$key] : $relationship_model->get_ui_column_names(array(moojon_primary_key::get_foreign_key($model->get_table()))); $child_attributes = array_key_exists($key, $attributes) ? $attributes[$key] : array(); $child_no_records_message = array_key_exists($key, $no_records_messages) ? $no_records_messages[$key] : array(); $div->add_child(table_for($value, $child_column_names, $child_attributes, $child_no_records_message, $count)); break; } } return $div; } else { return null; } }
protected final function belongs_to($name = null, $foreign_table = null, $foreign_key = null, $key = null, $shared_columns = array()) { $name = $name ? $name : $this->class; $foreign_table = $this->table; $foreign_key = $foreign_key ? $foreign_key : moojon_primary_key::get_foreign_key($foreign_table); $key = $key ? $key : moojon_primary_key::NAME; if ($this->has_property($name)) { throw new moojon_exception("Duplicate property when adding relationship ({$name})"); } if (!$this->has_column($key)) { throw new moojon_exception("No such column to use as key for relationship ({$key})"); } $relationship = new moojon_belongs_to_relationship($name, $foreign_table, $foreign_key, $key, $this->get_column($key)); $relationship->set_shared_columns($shared_columns); $this->relationships[$name] = $relationship; }
private function map_member_routes($uris = array()) { $id = array_shift($uris); $uri = implode('/', $uris); $id_property = $this->id_property; $params = array_merge($this->params, array($id_property => $id)); $relationship_routes = $this->get_relationship_routes(); $params['relationship_routes'] = $relationship_routes; $pattern = ''; $routes = array_merge(array_key_exists('member_routes', $this->params) ? $this->params['member_routes'] : array(), $relationship_routes); if ($routes && ($route = moojon_routes::map($uri, $routes, false))) { $new_params = $route->get_params(); if (array_key_exists('resource', $new_params)) { $id_property = moojon_primary_key::get_foreign_key($this->resource); $params[$id_property] = $id; } $pattern = ":{$id_property}/" . $route->get_pattern(); $params = array_merge($params, $new_params); } else { switch ($this->method) { case 'get': if (!$uri) { $pattern = ":{$id_property}/"; $params['action'] = 'show'; } else { if ($uri == 'edit' || $uri == 'delete' || $uri == 'show') { $pattern = ":{$id_property}/{$uri}"; $params['action'] = $uri; } } break; case 'post': $pattern = 'create'; $params['action'] = $pattern; break; case 'put': $pattern = 'update'; $params['action'] = $pattern; break; case 'delete': $pattern = 'destroy'; $params['action'] = $pattern; break; } } return $this->match_route($pattern, $params, false); }
public function add(moojon_base_model $model) { $key = $this->relationship->get_key(); $foreign_key = $this->relationship->get_foreign_key(); $accessor = $this->accessor; switch (get_class($this->relationship)) { case 'moojon_has_many_relationship': $model->{$foreign_key} = $accessor->{$key}; break; case 'moojon_has_many_to_many_relationship': if ($model->new_record) { $model->save(); } $class_name = $this->relationship->get_class($accessor); $class = new $class_name(); $key_property = moojon_primary_key::get_foreign_key($accessor->get_class()); $class->{$foreign_key} = $model->{$key}; $class->{$key_property} = $accessor->{$key}; $model = $class; break; } $this[] = $model; }