0

I want custom a few things in the library db...

And I created core system classes MY_DB in application/core folder:

class MY_DB extends CI_DB {
    public function __construct() {
        $CI =& get_instance();
        $CI->load->database();
        $this->db = $CI->db;
    }

    public function get($table = '', $limit = NULL, $offset = NULL) {
        // my code custom...
        $this->db->get($table, $limit, $offset);
    }
    public function insert($table = '', $set = NULL) {
        // my code custom...
        $this->db->insert($table, $set);
    }
}

but apparently it does not work.

I want when I execute $this->db->get(), it will run the code I customized within class MY_DB.

If you just take a look at my problem and share a bit of your science, I'd be very grateful. Thanks!

8
  • First of all, there is no core class named DB, therefore the override does not have to work. Secondly is your $config['subclass_prefix'] set to 'MY_'? No more ideas :/ Commented Jul 23, 2015 at 10:58
  • I just found something. Have a look at this. So as I mentioned there is no core class named DB, you need to work it out from Loader class as in this example https://github.com/bcit-ci/CodeIgniter/wiki/Extending-Database-Drivers Commented Jul 23, 2015 at 11:00
  • Yes, $config['subclass_prefix'] = 'MY_' has been set as default Commented Jul 23, 2015 at 11:00
  • 1
    You need to create instance $CI =& get_instance(); and use $CI->db->get() Commented Jul 23, 2015 at 11:08
  • I can do it (please see my edited), but apparently it does not work when I execute $this->db->get() Commented Jul 23, 2015 at 11:12

1 Answer 1

1

CodeIgniter (at this time, latest version 3.0.0) doesn't support extending its database classes like it does with other libraries. It's not impossible of course, but there's no official, built-into the framework way to do it and you'll have to come up with your own hack for it.

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

2 Comments

Currently, I can only create new class (my custom is extendDB), through it, I can execute $this->extendDB->get(), but this one was very inconvenient when I only want $this->db->get()... I really do not want to intervene directly in the core, as this would not be good if I want to upgrade the core later, or if there is interference on the core, I want it to be limited at least. You have a simple way to solve the problem of me?
I am certainly not suggesting that you modify the framework's core yourself, but I can't tell you more than that ... it's too broad.

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.