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

Thanks for all of your replies.  I am taking all of this in.


The reason I came up with what I did in the second example is because I envisage doing something like this:


PHP Code:
$this->load->model('customer_model');
foreach (
$query->result_array() as $row) {
   $c = new Customer_model();
   $c->create($row);
   $customers[] = $c;



Then in the view I could do something like this:


PHP Code:
foreach ($customers as $customer) {

   if ($customer->isBirthday()) :

       echo 'It is ' $customer->name ' birthday today';

   }



So in other words, the $customers array contains a list of customer_model objects, each one having access to all of the methods within the customer_model object.


This kind of leads on to another question, which is how should a list of objects be treated?


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?

(04-28-2016, 09:51 AM)kilishan Wrote: Personally, I would separate the Customer entity from the model itself. What I mean is that I would have two classes: Customer and Customer_model. The model would be responsible for all interactions with the database. Then, the Customer class would represent a single customer. It would have any additional business logic needed inside of it, functions to get related entities, etc. You can have the query result object return custom classes, so your code would simplify into something like this:

Interesting, I will digest what you just said and have a think about it
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB