CodeIgniter Forums

Full Version: Want class to encapsulate database records, where to put it?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]sneakyimp[/eluser]
I have written a class that I often use which allows me to create, fetch, and update records from a database by interacting with a class object that I instantiate. I want to make use of this in my latest CI project and am unsure a) where I should put my scripts in the CI file hierarchy and b) how to make sure I can instantiate a particular class with as little fuss as possible (ideally without having to include/require each class script separately). I'm hoping someone can tell me where to put my scripts so that they are most easily instantiated. Obviously, I want my scripts to be safe if I upgrade CI with a newer version.

For example, to create a db record in the table MyTable, I would create a new instance of a class DBObjMyTable like so

Code:
$obj = new DBObjMyTable( $dbconn,
  array(
    "column1" => "value1",
    "column2" => "value2",
    "column3" => "value3",
    "column4" => "value4"

  );
);
// this writes the data record in the db (or throws an exception on failure)
$obj->write_to_db();
// and the id gets set automatically by my class
echo "your new record was created with id=" . $obj->id;
I can easily generate a class for each of my db tables automatically.

Can anyone advise me?

El Forum

[eluser]Tpojka[/eluser]
View how other achieved that. Try to google like 'codeigniter crud'.

El Forum

[eluser]sneakyimp[/eluser]
OK I wrote a lengthy (and thoughtful!) post here which was obliterated by some rude anti-spam feature on this site, which really sucks.

Thanks for your suggestion I found a thing called simplemodel (by alexwenzel at bitbucket) which looks pretty awesome. drop it in your models folder and create an autoloader. i like that a lot better than having to load a library and passing all my constructor params via a single array. Also looking great is "simple crud" at Henri blog. I'd link these here but the mean anti-spam bouncer would probably kill my post again.

I'm still confused about the reason for using a model versus using a library, but I've been reading about it.

Also, why would you want to "set your own prefix?" I don't understand what this accomplishes.

El Forum

[eluser]davidMC1982[/eluser]
I think of it like this:

Any functionality that is specific to a data entity (database table, record etc) belongs in a model. All interaction with the database should happen through this model.

Any functionality that is generic (independent of a specific model) belongs in a library. This will include things like sending emails, authentication, generating pdf's from views etc etc.

Basically, if you find yourself repeating code in various controllers, it probably belongs in a model if it's entity specific or in a library of it's generic. And if it's just a simple function being repeated, stick it in a helper.

Regarding setting a prefix, this is useful to avoid database table name collisions between your tables, the tables required by libraries and - when on shared hosting - the tables of your other sites.