Welcome Guest, Not a member yet? Register   Sign In
Creating multiple instances of a model
#8

(04-28-2016, 10:16 AM)CINewb Wrote: For example, a single "customer" is an object or a model.  But what is a list of customers?  I think in some patterns this is referred to as a "repository" or "collection" perhaps.


My thinking is that "customer" could be a model (featuring name, dob, email, etc), and "customer_repository" could be a separate model (list of customers, total number of customers, etc).  This would however mean that $this->customer_repository_model->getList() would need to access $this->customer_model->get() to get the individual customers, and I'm not sure whether or not a model should access another model?

The example I gave is based on the Repository pattern. It basically states that you have an Entity and a Repository. The Entity exists completely on it's own and only represents the entity and it's business rules. It doesn't care how you get or save the data, but lives in its own little silo. The Repository, then, is responsible for creating and saving the items. In CI terms, that's the Model. The benefit is that you can always change out how the data is retrieved and saved, or even where it's saved, without affecting the application since the application only relies on Entity.

As for Collections, the are what they sound like, collections of that item. Often times, they will have additional functionality that is helpful, but I've found that 90% of the time an array works just as well as a Collection, with less abstractions, less work to duplicate the same features you get by having an array, less overhead, etc. Sometimes they're helpful, but I personally stay away from worrying about that until you need it.
Reply


Messages In This Thread
RE: Creating multiple instances of a model - by kilishan - 04-28-2016, 10:45 AM



Theme © iAndrew 2016 - Forum software by © MyBB