Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions & DataMapper OverZealous Edition
#1

[eluser]goliatone[/eluser]
Hi there,

Here is the situation: How to relate add-on modules' models with "global" models?


I have this application which uses some general models that reside on the top level modules directory ( application/models).
Then there are different modules ( i.e. pages ) which contains module specific models ( applications/modules/pages/page)

I want to create an advanced DMZ relationship (editor) between page and user such as:


Page:
Code:
class Page extends DataMapper {
    $has_one = array(        
        'editor' => array(
            'class' => 'user',
            'other_field' => 'edited_page'
        )
    );
}


User:
Code:
class User extends DataMapper {
    $has_many = array(        
        'edited_page' => array(
            'class' => 'page',
            'other_field' => 'editor'
        )
    );
}

How could I manage this dependency in a way that doesn´t break modularity? One benefit of having modules is that you can separate concerns, but since my Page is physically dependent on User by having to type its relationship with Page it breaks the deal.

If i know beforehand what modules i have, and what are they relationships with User, I can hard code them no problem. The issue is with modules that are supposed to be loaded to extend the application as an add-on(Wordpress like plugin).


One way I thought to solve the problem would be the following:
Since all the add-on modules have an install controller that create database tables and stuff like that, I could then edit the user.php file adding the dependencies of the add-on.


Another approach, more complicated, would be to use a DDBB table containing the all the relationships, and recreate a php file every time the table is modified and in the user.php include that file.


I'm sure some of you faced the same problem before, what where your solutions? How would you do this?



ps:
Sorry is this issue is addressed somewhere else in the forums or wiki, I did search but found nothing relevant.
#2

[eluser]dejavu[/eluser]
You'll have to patch the autoload function in datamapper.

Here's my code:

Code:
// Prepare path
        if (isset($CI->load->_ci_model_paths) && is_array($CI->load->_ci_model_paths))
        {
            // use CI loader's model path
            $paths = $CI->load->_ci_model_paths;
        }
        else
        {
            //$paths = array(APPPATH);
            /*
             * Added to allow for modular extensions
             */
            $paths = array();
            $dir = opendir(APPPATH.'modules/');
            if ($dir) {
              while (false !== ($file = readdir($dir))) {
                if ($file[0] == '.') continue;
                $filename =  APPPATH . 'modules/' . $file . '/';
                if (is_dir($filename)) {
                  $paths[] = $filename;
                }
              }
            }
            $paths[] = APPPATH;
        }

However, CI uses some autoload magic itself to load $CI->db. So often you'll get errors like: Call to a member function last_query() on a non-object...

My way around it that works sometimes is get_class_methods($this->CI->db) or $this->db or whatever. That seems to trigger the autoloading.

I'm trying to figure out how to force the database load instead of relying on autoload, or perhaps get CI's autoload of the db to work properly.




Theme © iAndrew 2016 - Forum software by © MyBB