Welcome Guest, Not a member yet? Register   Sign In
Datamapper best practice
#1

[eluser]davidMC1982[/eluser]
I have the following code in my controller:

Code:
class Products extends CI_Controller {

public function list_products($manufacturer_id = null) {
  if($manufacturer_id) {
  
   $products = new Product();
   $data['products'] = $products->where('manufacturer_id', $manufacturer_id)->get();
  }
  else {
  
   $products = new Product();  
   $data['products'] = $product->get();
  }
      
   $this->load->view('templates/header');
   $this->load->view('list_products', $data);
   $this->load->view('templates/footer');
}
}

And the following in my model:

Code:
class Product extends Datamapper {

var $has_one = array("manufacturer");
var $has_many = array("orderdetail, item, rate");

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }


}

Obviously, the code is intended to list all the products in the database, by manufacturer if given and pass these objects to the view.

Best practice suggests fat models and thin controllers. However, when using an ORM such as Datamapper the "fat model" part seems to be mostly taken care of automagically. My question is, in this case, how should the code above be altered?
#2

[eluser]WanWizard[/eluser]
Imho you should still go for the fat model approach. Which means methods in the model that deal with data processing, method calls in the controller to fetch the data.

Ofcourse, with an ORM there's going to be a gray area, especially when it's about simple get's like this.
#3

[eluser]davidMC1982[/eluser]
Thanks for the answer.

Can a Datamapper model work with a MySQL view?

Many Thanks.
#4

[eluser]WanWizard[/eluser]
I never tried, but I don't see why not. As long as the result includes an 'id' field that can be used as primary key.




Theme © iAndrew 2016 - Forum software by © MyBB