Welcome Guest, Not a member yet? Register   Sign In
Where to put plain object classes?
#41

(01-05-2015, 01:25 PM)mwhitney Wrote: You'll probably never find a satisfactory generalized answer to the question of which classes are libraries and which are models. I generally think of libraries as a location for things which tend to interact with multiple parts of the MVC pattern, or don't easily fit into the pattern. I also frequently use it for code which was not originally written for an MVC framework in the first place, or even libraries originally written for other frameworks, even if they are MVC frameworks.

Models I tend to use primarily for data access, whether the data happens to be in a database or some other form. I tend to put my business rules into models, but I rarely have reason to store the business rules separate from the model(s) which interface the data to which those rules apply. In many cases I also include methods in my models which allow those rules to be retrieved or executed by the controller (more often than not because it simplifies working with certain libraries in CI, like form_validation).

I really like this thoughtful post.

I'm mostly thinking of my controllers as a receptionist at a hotel or restaurant. They help you find the right room or table and are knowledgeable about who knows what, but lack specialized skills to clean your room or cook your meal. They locate the responsible code and bring it to bear on the request at hand. These should be highly aware of security considerations.

I'm thinking models are our code. I.e., our code as distinct from libraries supplied by others. Things that pertain to our system and nothing else. Obviously this would encapsulate our data. It would also encapsulate our rules about who can do what when and who cannot. Or what data we create when certain actions are taken, etc. I expect most of our custom-written code to exist here.

Libraries seem to me to include third-party stuff like classes to send Email or maybe third party encryption tools or OAuth stuff or Paypal/Authorize.net/Amazon Payments/Google Checkout classes. Anything general purpose libraries (i.e., that handle email, SMS, FTP clients, CDN connections, cloud computing access, etc.) should go in the libraries folder. And generally speaking, I expect any interaction with these classes will probably be encapsulated by some of my model code. More precisely, I probably won't have much, if any, access to these libraries in my controllers. Whether or not I send a send someone an email or upload a file to a CDN seems much more a my system decision than a simple request-routing decision and is therefore in the Model domain.

At any rate, those are my thoughts now. Specific anecdotal issues that shatter this ideal are welcome.
Reply
#42

(01-05-2015, 12:49 PM)sneakyimp Wrote:
(01-01-2015, 12:30 PM)includebeer Wrote: What? I don't know what you have done but you don't need to include anything to use a model!
You are incorrect, sir! If you want to extend CI_Model, you must either
a) include/require system/core/Model.php explicitly
OR
b) call $this->load->model() to get CI to require/include the CI_Model class for you.

To prove, try downloading a fresh CI3 from here:
http://www.codeigniter.com/download

Edit the Welcome controller so it's index function looks like this:

Code:
       public function index()
       {
               require_once APPPATH . "models/Testmodel.php";
               $v = new Testmodel();
               $v->foo();
       }

If you don't know how to load a model, read the documentation. It looks like you want to use CodeIgniter but refuse to learn how it works or you try to reinvent how it's supposed to work. If you know it needs "$this->load->model()" why are you wasting our times telling us you know you need to call load->model, but you refuse to call it and then complain it doesn't work.
Reply
#43

OK I'll admit that I can see from the documentation here:
http://www.codeigniter.com/userguide3/ge...odels.html
that you can certainly load a model without an include or require statements. However, this results in CI trying to instantiate the model. Unless I'm missing something, the documentation doesn't describe any way that I might supply any parameters to the constructor of the Model class. If this is possible, it would be helpful information. If it is not possible, that would also be helpful information.
Reply
#44

(This post was last modified: 01-06-2015, 05:35 AM by Jamie.)

(01-05-2015, 10:18 PM)sneakyimp Wrote: OK I'll admit that I can see from the documentation here:
http://www.codeigniter.com/userguide3/ge...odels.html
that you can certainly load a model without an include or require statements. However, this results in CI trying to instantiate the model. Unless I'm missing something, the documentation doesn't describe any way that I might supply any parameters to the constructor of the Model class. If this is possible, it would be helpful information. If it is not possible, that would also be helpful information.

I may have misunderstood your question here - apologies if this is the case, but take a look at Loader Class Documentation about passing parameters to models.

Edit: Just noticed that this had already been suggested by includebeer here.
Jamie Bond
Full time International Development undergraduate.
Part time website developer.
Reply
#45

(01-06-2015, 05:18 AM)Jamie Wrote: I may have misunderstood your question here - apologies if this is the case, but take a look at Loader Class Documentation about passing parameters to models.

Edit: Just noticed that this had already been suggested by includebeer here.

That applies to the loading of libraries only and you are limited to just one parameter. Unless I am missing something, you cannot supply any parameters at all when loading a model. Also, I find it curious that CI doesn't use call_user_func_array for loading libraries so that one can specify multiple parameters to a library constructor that accepts multiple parameters.

I fully recognize that CI is designed the way it is and that one must get used to certain peculiarities with any framework. One of the nice things about CI as I've seen it is that it seems pretty flexible in that it doesn't force you into any particular coding mode unnecessarily. As for the question "Where to put plain object classes?" I think the following are pretty important information when deciding where to put one's classes:
1) If you use CI's loader to load your libraries, they must be designed such that their constructor accepts at most one parameter
2) If you use CI's loader to load your models, their constructors must expect no parameters
3) If you use CI's loader to load your libraries or models, they are going to be automatically instantiated
4) If you plan to extend CI_Model, you must either load some model using the loader or you must explicitly include/require the CI_Model class which is found in system/core/Model.php
Reply
#46

(This post was last modified: 01-06-2015, 03:08 PM by Jamie.)

(01-06-2015, 01:04 PM)sneakyimp Wrote:
(01-06-2015, 05:18 AM)Jamie Wrote: I may have misunderstood your question here - apologies if this is the case, but take a look at Loader Class Documentation about passing parameters to models.

Edit: Just noticed that this had already been suggested by includebeer here.

That applies to the loading of libraries only and you are limited to just one parameter. Unless I am missing something, you cannot supply any parameters at all when loading a model. Also, I find it curious that CI doesn't use call_user_func_array for loading libraries so that one can specify multiple parameters to a library constructor that accepts multiple parameters.

I stand corrected, yes that applies to library only. With that said the Loader class can be extended to allow for this.
Jamie Bond
Full time International Development undergraduate.
Part time website developer.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB