CodeIgniter Forums
Datamapper best practice - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Datamapper best practice (/showthread.php?tid=55789)



Datamapper best practice - El Forum - 11-09-2012

[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?


Datamapper best practice - El Forum - 11-09-2012

[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.



Datamapper best practice - El Forum - 11-15-2012

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

Can a Datamapper model work with a MySQL view?

Many Thanks.


Datamapper best practice - El Forum - 11-16-2012

[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.