Q:

Structure functions in Codeigniter

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!

A:

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:
abstract class table_model extends CI_Model
{
  function __construct()
  {
    parent::__construct();
  }

  public function write_all()
  {
    // do some stuff to save a set of data
    // maybe add some logging in here too, if it's on development
    // and how about some benchmarking for performance testing too
    // you get the idea
  }
}

Next, create a class that extends table_model but with functions specific to the Accounts table.
public class accounts_model extends table_model
{
  function __construct()
  {
    parent::__construct();
  }

  public function get_everything()
  {
    // whatever it takes to get everything...
  }
}

Finally, you can do stuff like...
$this->account_model->get_everything();
$this->account_model->write_all();

And if you have another model (my_model) you can also do:
$this->my_model->get_just_a_few_things();
$this->my_model->write_all();

