Welcome Guest, Not a member yet? Register   Sign In
Want class to encapsulate database records, where to put it?
#1

[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?


Messages In This Thread
Want class to encapsulate database records, where to put it? - by El Forum - 10-17-2013, 04:21 PM



Theme © iAndrew 2016 - Forum software by © MyBB