CodeIgniter Forums
My_Model don't auto Load by Core/Loader - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forum-17.html)
+--- Thread: My_Model don't auto Load by Core/Loader (/thread-64201.html)



My_Model don't auto Load by Core/Loader - Pad - 01-26-2016

Hello,

I use Code Igniter 3.0.4 and I have a class MY_Model.php in repository application/core/ but when i call a model, the class MY_Model don't load.

With Code Igniter 3.0.0 this isn't not a bug !

I saw in the file /system/core/Loader.php at line 315 and there is the expression "require_once .... "  but not the expression new $class(); with in $class = "MY_Model"

I added to the line 320 and it goes !
Code:
$CI->$class = new $class();
My Class "My_Model"  is charged as soon as I call a model extends CI_Model

Can you take into account this modification for next release of Code Igniter 3 ?


RE: My_Model don't auto Load by Core/Loader - Narf - 01-27-2016

No, it's not supposed to be instantiated. See this bug report: https://github.com/bcit-ci/CodeIgniter/issues/4350


RE: My_Model don't auto Load by Core/Loader - Narf - 01-27-2016

And to clarify further: MY_Model ("MY", not "My" - this is important) is getting loaded, contrary to your claim ... I suppose you mean something else.


RE: My_Model don't auto Load by Core/Loader - Pad - 01-27-2016

File : application/core/MY_Model.php


Code:
class MY_Model extends CI_Model
{

  // creer ici une propriete avec le nom de la database
  private $commun;

  public function __construct()
  {
    parent::__construct();
    log_message('debug', __CLASS__ . " Class Initialized");
   $this->commun = $this->load->database('commun', TRUE, TRUE);

    // Pass reference of database to the CI-instance
    $CI = & get_instance();
    // Creation d'une propriété dans la controlleur qui permet d'y acceder rapidement n'importe ou
    // $CI->my_data_base = & $this->my_data_base;
    $CI->commun = & $this->commun;
  }
}


On CI V3.0.0 I have the message "MY_Model Class Initialized" in the log.txt
On CI V3.0.4 I haven't this message, but with my code in system/core/Loader.php that's fine !

??

Thanks !


RE: My_Model don't auto Load by Core/Loader - mwhitney - 01-27-2016

You shouldn't need the MY_Model class to be instantiated in order for things to work properly. Whatever class extends MY_Model should simply call the parent::__construct() method in its constructor (if the constructor is defined) just as you have done in your MY_Model constructor.

Further, you shouldn't need to call get_instance() in MY_Model, since CI_Model defines a __get() method which does this for you. If you removed the definition of the $commun property from MY_Model, calling $this->commun = $this->load->database('commun', true, true); should do the same thing that you seem to be trying to do with the rest of the code in your constructor.