Welcome Guest, Not a member yet? Register   Sign In
Model in subfolder with same name as model in main folder
#1

I noticed that if there is a model in a subfolder with the same name as model in the main folder, it is ignored in favor of the model in the main folder.

How to reproduce:
Download and install CodeIgniter 3.1.2

/application/models/Blog_model.php:
PHP Code:
class Blog_model extends CI_Model {

 
   public function hello()
 
   {
 
       echo "<p>Hello from the main Blog_model!</p>";
 
   }


/application/models/legacy/Blog_model.php:
PHP Code:
class Blog_model extends CI_Model {

 
   public function hello()
 
   {
 
       echo "<p>Hello from the legacy Blog_model!</p>";
 
   }


/application/controllers/Welcome.php:
PHP Code:
class Welcome extends CI_Controller {

 public function 
index()
 {
 
       $this->load->model('blog_model');
 
       $this->load->model('legacy/blog_model','legacy_blog_model');
 
       $this->blog_model->hello();
 
       $this->legacy_blog_model->hello();
 }


Result: the main blog_model gets called twice:

Quote:Hello from the main Blog_model!

Hello from the main Blog_model!

Aliasing the main one like this doesn't help:

PHP Code:
        $this->load->model('blog_model','main_blog_model');
 
       $this->load->model('legacy/blog_model','legacy_blog_model');
 
       $this->main_blog_model->hello();
 
       $this->legacy_blog_model->hello(); 

Oddly enough, if I comment out the lines for the main blog model, THEN the blog model in the subdirectory gets called:

PHP Code:
        // $this->load->model('blog_model');
 
       $this->load->model('legacy/blog_model','legacy_blog_model');
 
       // $this->blog_model->hello();
 
       $this->legacy_blog_model->hello(); 

Quote:Hello from the legacy Blog_model!

My planned workaround is to rename the models in the subdirectory, eg. /models/legacy/blog_model becomes /models/legacy/legacy_blog_model but at that point I might as well move it up to /models. (admittedly, that might have been the best way all along; no need to alias anything.)

Is this a genuine issue or am I making a mistake?
Reply


Messages In This Thread
Model in subfolder with same name as model in main folder - by Tom S - 01-06-2017, 10:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB