Welcome Guest, Not a member yet? Register   Sign In
Models that use other models? array of models? objects?? a case study
#1

[eluser]avastreg[/eluser]
Hi guys, i've just get into codeigniter and MVC, very good work!

I'd want to know what is the best way to solve my problem thinking with MVC pattern, and to find the best practices in these cases.

Let's start with the description of my case of study:

I have to list in my app many products. Every products is a record of table products, who has as fields id and name of the product. On a product user can do many actions, like add for the first time the product, re-add the product, consider shipped/unshipped that etc. I have a related table for this, called my_product_actions, on which i have as fields an action, a product_id (foreign key to products table), date of the action etc.

Now, theoretically, i think that the best way to retrieve and show products in OOP methodology is to build a class named Product, with gets the data from both tables, so when i get a product i can have the actions' history of that product, it's name etc.

I retrieve only the subset of products I'm interested in, instantiating so many Product objects as i need.

In MVC, what's the best way to achieve it?

Ok, i can transform the Product class into a Product_model.

But then?? I have to create another model, called Products_model, who call various instances of Product_model??

Or i have to call directly the Product_model from the controller? And, in that case, i need more products, one model is not enough!

Please, give me your feedback, because i don't have a clear idea to do this with MVC pattern.
#2

[eluser]TheFuzzy0ne[/eluser]
I'm having a very similar issue with forum design. I'm struggling to understand your description of the problem. Please could you post your table schema?
#3

[eluser]avastreg[/eluser]
Code:
-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `my_products` (
  `id` int(11) NOT NULL auto_increment,
  `name` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='product table' AUTO_INCREMENT=614 ;

-- --------------------------------------------------------

CREATE TABLE IF NOT EXISTS `my_link_actions` (
  `link_id` int(11) NOT NULL,
  `actions` varchar(255) NOT NULL COMMENT '\n- added\n- created \n- completed\n- uncompleted',
  `date` datetime NOT NULL,
  `source_ip` varchar(255) default NULL,
  `user_id` int(11) NOT NULL,
  PRIMARY KEY  (`product_id`,`operation`,`date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8


Here it is. The core of the problem is: if i have to list the products, and ideally a product (with its actions history) can be a model, how i have to manage this list??

I must call the Product_model for each product, so in the end i have an array of products?

Reading about models i find that almost in every case there is one model, one controller and one or more views.

I need those products as instances of the model! But many instances, because the classical instance retrieved by $this->load->model('Product_model'); is not enough. How i can do?
#4

[eluser]TheFuzzy0ne[/eluser]
You can have your product model pass back an array of product objects. So you can do something like:

Code:
$products = $this->products_model->get($start, $end); #  Where $start and $end are the limit and offset used for pagination.

Basically, you're model is what processes any input products, and interfaces with the database. Your objects can be fairly static, and don't have to have any methods - only properties.

I hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB