• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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

#2
CodeIgniter maintains its own list of loaded components, so it doesn't try to load the legacy/Blog_model, even though it locates it.

You need models, controllers and libraries (all classes) to be uniquely named.
Reply

#3
(01-06-2017, 11:19 AM)ciadmin Wrote: CodeIgniter maintains its own list of loaded components, so it doesn't try to load the legacy/Blog_model, even though it locates it.

You need models, controllers and libraries (all classes) to be uniquely named.

Thank you for replying so quickly and providing clarification. Now I understand what is really going on.
Reply

#4
... and this is why we want namespaces. Smile
Reply

#5
Code-igniter maintains its own list of loaded components. But for you can make another folder for access blog_model

$this->load->model('admin/blog_model');

$this->load->model('legacy/blog_model','legacy_blog_model');


If you are use this structure than its working fine.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.