Want class to encapsulate database records, where to put it? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Want class to encapsulate database records, where to put it? (/showthread.php?tid=59536) |
Want class to encapsulate database records, where to put it? - El Forum - 10-17-2013 [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, Can anyone advise me? Want class to encapsulate database records, where to put it? - El Forum - 10-17-2013 [eluser]Tpojka[/eluser] View how other achieved that. Try to google like 'codeigniter crud'. Want class to encapsulate database records, where to put it? - El Forum - 10-18-2013 [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. Want class to encapsulate database records, where to put it? - El Forum - 10-18-2013 [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. |