EllisLab text mark
Advanced Search
     
Datamapper best practice
Posted: 09 November 2012 05:46 PM   [ Ignore ]
Joined: 2012-11-09
39 posts

I have the following code in my controller:

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:

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?

 
Posted: 09 November 2012 06:00 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

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.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 15 November 2012 09:23 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-11-09
39 posts

Thanks for the answer.

Can a Datamapper model work with a MySQL view?

Many Thanks.

 
Posted: 16 November 2012 04:56 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

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.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM