Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
LSB bug? Or when can I use ORM models in init process 1.5.x
  • Hey I have a mega problem to use ORM models in the init process of fuel. In my application I overload the allways_load method and add a separat module initializer:

    public static function always_load($array = null)
    {
    parent::always_load($array);
    \Module::init_modules();
    }

     In \Module::init_modules() I use a method named: register_modules which will load modules from database (ORM) 

    public static function register_modules()
    {
    $modules_in_rdb = \Model_Module::find_all();
    $modules_in_dir = static::get_module_paths();
    $modules_diff = array_diff($modules_in_dir, $modules_in_rdb->get_module_names_array());

    if (!empty($modules_diff)) {
    \Logger::forge()->addInfo('Register new Modules', array(count($modules_diff)));
    foreach ($modules_diff as $module_name) {
    static::add_new_module($module_name);
    }
    }
    }

    The line:
    $modules_in_rdb = \Model_Module::find_all();
    will generate a endless loop and if I analyze the code back this method:
    public static function forge($data = array(), $new = true, $view = null)
    {
    return new static($data, $new, $view);
    }
    will break my skript. When can I use ORM models after loading the package?
  • what does find_all() do? That is not an ORM method....
  • find_all is a simple wrapper method to find:
    public static function find_all(array $options = array())
    {
    return static::find('all', $options);
    }
  • I don't see why or where that would create an endless loop.

    Forge returns a new model object, that's what it does. There is no way it will call forge() again.
  • Hey thx for your reply. I have the feeling that ORM - observers didn't work in init progress of fuel. If I do the same operations in the controller level it works...?   
  • I don't see why that would not work, the framework is initialized at that point, the only thing not setup is the locale.

    It is a very complicated setup though, it would be a lot more logical to not overload Fuel and Module, just create a separate class, and put your code in the _init() method of that class. Then "always_load" that class.

    The best solution is to have that _init() register an event action on the "app_created" event. Fuel will then call it as soon as it's initialisation is finished.

    If you have a loop, you have an error and a backtrace. So should be able to analyse the backtrace to see where it goes wrong.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion