![]() |
Should I go about this another way? Currently a Library, Should be Model? - 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: Should I go about this another way? Currently a Library, Should be Model? (/showthread.php?tid=16538) Pages:
1
2
|
Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]internut[/eluser] Hey All, I am basically needing to get a bunch of record counts and feed them to a view. I need these count values (vars) returned to be available on many different pages. What I'm doing now is I have a "user_count" library (User_count.php) which is auto loaded. The code is (I made it shorter as it repeats based on a table field): Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Should I put this into a model and auto-load it? I've tried a few times with no luck putting it into a model and getting the values back to use in my views. I'm trying to move all database calls into models but having no luck here. What I have now in a Library is working perfectly. Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]TheFuzzy0ne[/eluser] Whatever makes you happy. Your library works, it keeps database calls out of the controller, and it serves a very specific purpose. It would probably be a pain in the behind to split up such a small amount of code into a model and a view. I think that so long as it's you working on the code (and you don't have an HTML designer), you've done the right thing. One thing though, you have a typo: You've typed "Caneled" instead of "Cancelled". Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]Stu Green[/eluser] Hey! I had the same struggle when learning about MVC - is a library a Model or not? Well I work off the basis that if it's going to be reused across the WHOLE site and possibly even on other sites which are unrelated then make it a library. For example I'm building a permissions system (which I'll probably blog about this week) which can be used across other sites, it's a library. However if it's local to a particular area of the site and is mostly for database calls, then make it a model. I think I'm right in saying that if your code that you posted is a model, then you don't need the CI instance, you can just say $this->session->userdata('whatever') for example, no need for CI instance. Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]internut[/eluser] Thanks. guys. I've written a good majority of this program and am learning the better ways to go about things so I've am really back-tracking and making use of the MVC way. I'm just getting crazy thinking anything with a DB call in it should go into a model. I guess I'm not wrong, not right but in the middle. Thanks for the typo correction, you're the second to spot that ![]() The code posted is a library not a model so I went the CI Instance route. I REALLY appreciate your guys thoughts. So thanks and thanks again! Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]internut[/eluser] Another question if I can get a second of your time. Is it wrong to do: $this->user_count = $count; in a library and use: $this->user_count within a view? The value comes into the view fine, but is it wrong or right or none of the above to first throw it into: $data['user_count'] = $this->user_count; ? Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]Stu Green[/eluser] I think it all depends on the structure of your site, and how fussy you are about semantics and tidiness of code. The MVC approach was made because developers wanted nice tidy code and wanted to separate it out between backend stuff and frontend stuff (developers and designers dont always get along!). So if you don't mind a designer who has access to your view files having to use $this->yourlib->yourvar which comes from the library, as oposed to setting the variable in the controller and passing that to the view, then do what you like! :-) Its up to you. I'm all for clean and tidy code! Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]TheFuzzy0ne[/eluser] Welcome to the CodeIgniter community, haloweb! I too struggle to understand what a library should be used for. Generally, all of the libraries I make are independent. I see MVC as, well MVC. The controller utilises models and views. I don't feel that a library fits in there, which is why I tend to make them self contained, and non-dependent on views or models. Generally, they're only accessed by the controller. I'd have to say that a library isn't a model. If it was, then it should be a model not a library. For me, a library represents an object, or a collection of methods and properties that all have something specific in common. It can handle just data, or it can handle the visual aspects to do with that. However, I still try to ensure that my libraries don't output data directly, they only return it. With that said, I'm sure there may come a time when I will have to have a library that outputs HTML, but I've not yet found the need to do so. I would like to hear other peoples views (no pun intended), on libraries, and what they should and shouldn't do. Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]Stu Green[/eluser] I tend to think of the Library outside of MVC I agree. The Library sites outside of the MVC and interfaces optionally with it, kind of like it's running along side. Model for DB Controller for hanging everything together View for the HTML Library for anything else that could be useful to a set of MVC files. My take. K im going to bed. Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]TheFuzzy0ne[/eluser] [quote author="internut" date="1236663580"]Another question if I can get a second of your time. Is it wrong to do: $this->user_count = $count; in a library and use: $this->user_count within a view? The value comes into the view fine, but is it wrong or right or none of the above to first throw it into: $data['user_count'] = $this->user_count; ?[/quote] I'd say it's not good practice. The view should have no idea of the context in which variables are used, it should just have access to variables in it's global scope. This also helps make things cleaner, and is almost like your seal to say that you've checked and confirmed that the variables you're supplying are what the view expects. Again, people may disagree with me here, but it's just my opinion. Should I go about this another way? Currently a Library, Should be Model? - El Forum - 03-09-2009 [eluser]internut[/eluser] I think i'll go with the approach that a library can be a middle man of the MVC. I only have libraries doing certain things which in the end just give me a var output or control some things that are used often. They do have some DB calls but I'm getting as much out of them as I can. One or two libraries it just makes more sense to have a few simple DB calls in them. I do like as you said Fuzzy having in a view: echo $user_groups versus: $this->groups->list |