Welcome Guest, Not a member yet? Register   Sign In
Using Preloaded Model Via BaseController in Other Controllers
#1

Hi Everyone,

I am trying to use and/or extend a model which is loaded in the BaseController's initController() function which uses the RequestInterface.

I was using the below to set a variable in initController().
PHP Code:
new \App\Models\Base_layout_markup_model$request ); 

I wish to use this model globally, and have other models extend from it, so I can call its functions in the other model, however, nothing has come of it.  I get errors mentioning I need to set database properties, to references of NULL when calling the base model functions in the class that extends it.

The only way I get things to work is using this process:
  • A controller, (extends BaseController), has a constructor which loads a model for that Controller, passing in the services I need.
    PHP Code:
    new \App\Models\Site_home_model( \Config\Services::request() ); 
  • The Site_home_model extends the Base_layout_markup_model, which also has the using App\Models\Base_layout_markup_model;.  Then in this model I have to use a constructor to call the Base_layout_markup_model this way:
    PHP Code:
    public function __constructor$request )
    {
    $this->_base_layout_markup_mdl = new \App\Models\Base_layout_markup_model$request ); 

    so that I can call its functions.
I would have thought that since I am extending from that 'base' model, I would not have to instantiate it in the constructor?

I also thought the idea of preloading models in the BaseController would make them available globally?  Otherwise, each model that I load then has to load the base layout model if I want to use it.

What is the best practice of preloading a model in the BaseController, which has access to the RequestInterface, which is available globally in other models?
Reply


Messages In This Thread
Using Preloaded Model Via BaseController in Other Controllers - by Mr Lister - 10-09-2021, 11:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB