![]() |
Using MVC - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Using MVC (/showthread.php?tid=2718) |
Using MVC - El Forum - 08-21-2007 [eluser]Paul Scott[/eluser] Morning All, I am just about to start playing about with CodeIgniter in a project I'm going to be working on. I'm going to see if I can get a good enough feel for it to use it and learn it on-the-fly. My main worries about using CodeIgniter is that I struggle with sticking to the MVC structure (even though CodeIgniter is apparently not very strict about it) because I'm not quite sure how to work this. I understand that Models are used for interaction with the database, Views are used for outputing the HTML and Controllers pull the page together. I'm not really sure how to port parts of my code, specifically classes that I have written (which, yes, interact with the database). Lets say I'm using the following (mock) class: Code: class Member { Before using MVC I would do the usual Code: $member = new Member($id); I've had a read around the forums and I believe what I'm looking for is Code: $this->CI =& get_instance(); // off the top of my head Thanks, Paul Using MVC - El Forum - 08-21-2007 [eluser]Sector[/eluser] This is perhaps not such a direct reply but here goes: Sometimes when you port sites to CI, you'll have classes that don't exactly fit, or worse, can't be transformed into MVC pattern without a crapload of work and violation of the Don't Repeat Yourself rule. I put these things into the application/libraries folder and autoload them. Works wonders. For example, I had a class calledd Api that contained about 40 methods all concerning database queries (f. example: getProject($id), deleteProject($id), ...) Instead of trying to forge it into models, I just renamed the class to Data and put it in my libraries. Works without having to change a thing in the code. Now I just call $this->data->getProject($id). You'll have to load the database class before the Data class though, in the autoload array ![]() Hope that gives a little insight. (it all depends on the magnitude of what you are trying to port, ofcourse) Using MVC - El Forum - 08-21-2007 [eluser]Paul Scott[/eluser] Thanks for that info. It doesn't exactly answer my queries because the situation is slightly difference. My code (I think) does fit into MVC - I'm just not sure how to work with using multiple instances of (what I believe should be) a model class. Edit: Perhaps that wasn't especially clear in my original post. Thanks for your input, all the same. Paul Using MVC - El Forum - 08-21-2007 [eluser]deviant[/eluser] I think there is a similar discussion in this thread http://ellislab.com/forums/viewthread/58506/ Using MVC - El Forum - 08-21-2007 [eluser]Paul Scott[/eluser] I found what I was looking for in the documentation (funnily enough, eh?) I guess I just over-looked it when I was originally trying to find what I was after - doubtful that it would be in there anyway - but I found it at http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html Thar be some good documentation with CodeIgniter. Might just need to print it all off and read it on the bus to/from work. Thanks for input Using MVC - El Forum - 08-21-2007 [eluser]Matthew Pennell[/eluser] [quote author="deviant" date="1187709254"]I think there is a similar discussion in this thread http://ellislab.com/forums/viewthread/58506/[/quote] Indeed. The answer to the OP is that Code Igniter doesn't think of models as "an instance of an object representing a record from the database" - models are just places to put your functions that deal with database access. I've written an Active Record implementation that is closer to what you're talking about - I'll post more about it soon. |