i´m trying to structure my functions in codeigniter to stay on top of things. Basicly i can make something like that:
$this->my_model->get_everything();
$this->my_model_db->write_all();
But of course i end up making and loading many files. I´d rather structure it like my JS-code and extend my models:
$this->my_model->db->write_all();
It´s the most logical and readable solution to me. I tried it but i´m not that good with PHP objects and classes (yet). Is there an simple way to achive this? Or is there a more practical solution? Thanks!
I think you are doing it backwards.
You can create multiple models that extend the built in CI_Model class with the general functions you want. Then you can inherit from those new classes for specific implementations.
For example, let’s say you’re working with a db table name Accounts
First, create a class that extends CI_Model that contains general functions for working with a set of data (CI_DB_Result, an array of models, an array of arrays, etc). Something like:
Next, create a class that extends table_model but with functions specific to the Accounts table.
Finally, you can do stuff like…
And if you have another model (my_model) you can also do: