Derokorian;11044553 wrote:Can't say I have an answer for you, but that maybe comes from the fact that I have a differing opinion about what models are for. I believe models are for interacting with data stores. I use a model to read from the db and change the data to a non-db format if necessary. I use it to write to a db, validating and modifying it for this specific store (not necessarily this specific data, I may store it in 2 places and require different formats or rules to be followed),
This all sounds great to me. I'm not sure what I might have expressed that would cause us to think differently about any of these points, although I do have a vague feeling that my idea of what belongs in MODEL may be considerably larger than your ideas. I've hard a lot of talk about 'fat models, skinny controllers' being ideal. If you could elaborate, I'd appreciate it. I'm hoping to learn here.
Derokorian;11044553 wrote:instead the controller will validate the web request meets the requirements of this API end point.
That sounds reasonable I suppose. On the other hand, the question "what is valid data for this data object?" seems best addressed to the model which must enforce data integrity and, as I understand it, "business logic". A similar question that seems entirely specific to the Model might be "can user X change data point Y." In my reckoning, whether or not the request "meets the requirements of the API end point" would be a question in which the controller's role is limited. It would analyse the incoming request, check a few authentication params (all provided by some model of course), make sure the request was GET or POST as appropriate, perhaps check encoding, and then route any input data to the appropriate model which would do the heavy lifting. The Model would then return the data which the controller would dutifully digest and route to the appropriate appropriate view.
The problem is that all these descriptions I'm providing are vague and don't really clear much up π
Derokorian;11044553 wrote: I use models to send emails, but not to compose the emails or the list of recipients.
It seems pretty apparent to me that a controller has some responsbility in formulating communications to a user -- taking care not to expose sensitive error conditions to potentially malicious users, for instance -- however, other types of communication seem like none of the controller's business. For instance, say a newsletter must be sent to all customers about a sale. This event might be triggered by a controller interpreting a user action, but the actual work of the email sending should probably be delegated to a library which interacts with the model to collect all the customer's email addresses. The whole job should furthermore be performed asynchronously rather than forcing the user to wait 24 hours for 100,000 emails to be sent.
Derokorian;11044553 wrote:Based on your post in the discussion you linked, I would say we use models differently. I would say that the way I make use of the Model layer, requires an instance whenever I use it. I tried to rap my brain for a reason not to need an instance, and I came up a bit empty.
OK I am having trouble with this too -- thus my post posing the question. How about the Factory pattern? Or some other singleton class?
http://www.phptherightway.com/pages/Design-Patterns.html
Derokorian;11044553 wrote:I agree, however, that you should be able to have multiple instances, and moreover be able to pass in an arbitrary number of arguments.
This is also for NogDog. I'm not 100% certain yet, but I believe that one cannot pass any parameters to a model in CI when loading a model like this:
class Mycont extends CI_Controller {
public function index() {
// this instantiates the Model class Mymodel, but there is no way to supply params
$this->load->model("Mymodel");
}
}