public final function get_table(moojon_base_model $accessor = null)
 {
     if (get_class($this) == 'moojon_has_many_to_many_relationship') {
         $return = $accessor->get_table() == $this->foreign_table ? $this->name : $this->foreign_table;
     } else {
         $return = $this->foreign_table;
     }
     return moojon_inflect::singularize($return);
 }
Esempio n. 2
0
function get_primary_key_id_property(moojon_base_model $model)
{
    $resource = moojon_inflect::pluralize(get_class($model));
    if (moojon_routes::has_rest_route($resource)) {
        $rest_route = moojon_routes::get_rest_route($resource);
        return $rest_route->get_id_property();
    } else {
        return moojon_primary_key::NAME;
    }
}
Esempio n. 3
0
 public static function scaffold($app, $model, $controller)
 {
     self::try_define('APP', $app);
     self::try_define('CONTROLLER', $controller);
     if (moojon_routes::has_rest_route($model)) {
         $route = moojon_routes::get_rest_route($model);
         if (APP != $route->get_app()) {
             throw new moojon_exception("Scaffold app & route app must be the same (" . APP . ' != ' . $route->get_app() . ")");
         }
         $id_property = $route->get_id_property();
     } else {
         $id_property = moojon_primary_key::NAME;
         self::add_route("new moojon_rest_route('{$model}', array('app' => '" . APP . "')),");
     }
     $swaps = array();
     $swaps['plural'] = moojon_inflect::pluralize($model);
     $swaps['singular'] = moojon_inflect::singularize($model);
     $swaps['Human'] = str_replace('_', ' ', ucfirst(moojon_inflect::singularize($model)));
     $swaps['human'] = str_replace('_', ' ', moojon_inflect::singularize($model));
     $swaps['Humans'] = str_replace('_', ' ', ucfirst(moojon_inflect::pluralize($model)));
     $swaps['humans'] = str_replace('_', ' ', moojon_inflect::pluralize($model));
     $swaps['id_property'] = $id_property;
     $views_path = moojon_paths::get_project_views_app_controller_directory(APP, CONTROLLER);
     moojon_files::attempt_mkdir($views_path);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'controller.template', moojon_paths::get_project_controllers_app_directory(APP, CONTROLLER) . "{$controller}.controller.class.php", $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . '_form.template', $views_path . '_form.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'new.view.template', $views_path . 'new.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'delete.view.template', $views_path . 'delete.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'index.view.template', $views_path . 'index.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'show.view.template', $views_path . 'show.view.php', $swaps, false, true);
     self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'edit.view.template', $views_path . 'edit.view.php', $swaps, false, true);
 }
Esempio n. 4
0
 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;
 }
Esempio n. 5
0
 protected final function add_relationship($relationship_type, $name, $foreign_table, $foreign_key, $key)
 {
     if ($this->has_property($name)) {
         throw new moojon_exception("Duplicate property when adding relationship ({$name})");
     }
     $key = $key ? $key : moojon_primary_key::NAME;
     if (!$this->has_column($key)) {
         throw new moojon_exception("no such column to use as key for relationship ({$key})");
     }
     $foreign_table = $foreign_table ? $foreign_table : moojon_inflect::pluralize($name);
     $foreign_table = self::strip_base($foreign_table);
     $foreign_key = $foreign_key ? $foreign_key : moojon_primary_key::get_foreign_key($foreign_table);
     $this->relationships[$name] = new $relationship_type($name, $foreign_table, $foreign_key, $key, $this->get_column($key));
 }
Esempio n. 6
0
function model_from_id($symbol)
{
    $class = moojon_inflect::singularize($symbol);
    $id = moojon_primary_key::NAME;
    $model = new $class();
    $method_name = "read_by_{$id}";
    return $model->{$method_name}(moojon_request::get($id));
}
Esempio n. 7
0
 public static function get_model_upload_directory(moojon_base_model $model, $public = false)
 {
     $class = moojon_inflect::pluralize(get_class($model));
     $id_column = moojon_primary_key::NAME;
     $root = !$public ? self::get_uploads_directory() : self::get_public_upload_path();
     return "{$root}{$class}/" . $model->{$id_column} . '/';
 }
 public static function get_table($foreign_key)
 {
     return moojon_inflect::pluralize(self::get_class($foreign_key));
 }
Esempio n. 9
0
 private function get_relationship_routes()
 {
     $relationship_routes = array();
     $model_class = moojon_inflect::singularize($this->resource);
     if ($path = moojon_paths::get_model_path($model_class)) {
         require_once $path = moojon_paths::get_model_path($model_class);
         $model = new $model_class();
         foreach ($model->get_relationships() as $relationship) {
             $foreign_table = $relationship->get_foreign_table();
             if (moojon_routes::has_rest_route($foreign_table)) {
                 $relationship_route = moojon_routes::get_rest_route($foreign_table);
             } else {
                 $relationship_route = new moojon_rest_route($foreign_table, array_merge($this->params, array('controller' => $foreign_table)));
             }
             $relationship_routes[] = $relationship_route;
         }
     }
     return $relationship_routes;
 }