1

I need your help, please! I have 2 models with the same name

1.... application/admin/user/models/User_model.php, with this code:

 <?php 
        defined('BASEPATH') OR exit('No direct script access allowed');

        class User_model extends CI_Model

     ?>

2.... application/front/user/models/User_model.php, with this code:

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed');

    class User_model extends CI_Model
 ?>

Both classes without differences. I need to use application/admin/user/models/User_model.php from application/front/user/controllers/User.php

I tried to do the following changes: If I add in application/front/user/models/User_model.php the namespace like this code:

<?php namespace customers;
        defined('BASEPATH') OR exit('No direct script access allowed');

        class User_model extends \CI_Model
 <?

And from application/front/user/controllers/User.php, I call application/admin/user/models/User_model.php always gives me an error as application/front/user/models/User_model.php was not loaded.

If I delete the namespace from application/front/user/models/User_model.php my code always goes to this mode NEVER to admin/user/user_model. I need to AVOID change admin/user/User_model.php because a lot of classes uses this MODEL.I only can change front/user/User_model.php But I need to USE admin/user/user_model.

I call application/admin/user/models/User_model.php like this:

$this->load->model('user_model'); 

I need to load application/admin/user/models/User_model.php

Please help me!!!! Thx! Ani

1
  • Show your config.php, please. Might be you configured to load models from: front/user/models. Commented Feb 12, 2018 at 5:39

2 Answers 2

3

That is simply not possible, since Codeigniter doesn't support Namespaces

Usually you would see an error like

Fatal error:  Cannot declare class `User_model`, because the name is already in use

But Codeigniter prevents this, because it doesn't load any models which are already loaded. You can see this here.

So the only way out of your misery is to simply rename your models - even if you don't want to ;)

Sign up to request clarification or add additional context in comments.

Comments

2

Hope this helps you

You can created subdirectory in models like this :

application/models/admin/User_model.php

application/models/user/User_model2.php

Load the model like this:

   $this->load->model('admin/User_model');
   $this->load->model('user/User_model2');

you should assigned your model to a different object name you can specify it via the second parameter of the loading method:

Use them like this :

   $this->load->model('admin/User_model', 'admin_model');

   $this->admin_model->some_method();

   $this->load->model('user/User_model2', 'user_model');

   $this->user_model->some_method();

For more : https://codeigniter.com/user_guide/general/models.html

1 Comment

imho - that wouldn't work - if he uses the same class name it only get admin/User_model instantiated, and since CI doesnt support Namespaces the only way out here is to rename that class...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.