0

this is my first question after several years of founding good answers over here. I'm re-coding one of my work trying to use a more deep level of OOP aproach but i don't know if what i'm doing is "correct".

What i'm trying to do is a simple product creation but abstract enough so the other coders doesn't have to know what db use, or how to valid product data. Just simple create($attr) method like.

This is a basic scheme of the code.

abstract class Crud_model_abstract {

 # private vars
 # getter for private vars
 # setter for private vars
 # some basic methods
}

class Product_model_crud extends Crud_model_abstract {

 # private vars
 # getter for private vars
 # setter for private vars
 # CRUD methods (using phpactiverecord.com lib)
}

Then some classes for category, brand, etc with the same structure than Product_model_crud

Now my question is this: Should I create another model that compose the actual product (add category to it's table then brand to it's table.... then create the product and store it) and then call that model from a controller passing the params, or should I convert that big model (the one that compose the product) into a controller instead.

I belive that i should create a big model, but i'm not really sure about anything at this point. I've read tooooo much material about good practices in OOP and i have doubts in every step of the way :D

What i've understand (correct me if i'm wrong) its that i should have enough layers in my code to make it more clean and specific. But in some cases i can't see when enough is enough.

Maybe it's a stupid question (of course!!) but i don't know what a hell i'm doing anymore!! Thanks you!!

gdi
  • 9
  • 2
  • I'm finding it difficult to understand your problem – Galen Jun 16 '12 at 19:28
  • The question is how deep should i go in the abstraction. Should i create a model that create the whole object (create the category, brand, etc) or all that logic should be inside a controller insted. – gdi Jun 16 '12 at 20:01
  • Read this one more article about OOP: http://www.advantexllc.com/blog/post.cfm/how-oo-almost-destroyed-my-business :) BTW, have you considered using Doctrine/Propel/phpactiverecord/anything_else ? – biziclop Jun 16 '12 at 20:44
  • @biziclop thanks for the link, actually i'm using phpactiverecord as i mentonied in the code – gdi Jun 16 '12 at 21:31

1 Answers1

0

Just use one model to insert the products. For example:

class Products extends abstractClass {

public function addProduct($params) {
 // Insert
}

public function getProduct($id) {
 // Get a product
}

public function editProduct($id, $params) {
 // Edit
}

}

Your abstract class would specify which methods needs to be included in your child class. I think you are over-thinking the whole scenario.

You would then do:

$product = new Products();
$product->addProduct(array('id' => 5, 'name' => 'Dog toy'));
David
  • 2,365
  • 8
  • 34
  • 59
  • Yes but addProduct would need to crate the category if doesn't exists (calling the addCategory method on Catogory method) and then addProducts its not just about adding a product, that's why i need another layer – gdi Jun 16 '12 at 20:32
  • @gdi Create a new class that extends the abstract class for category management. – David Jun 16 '12 at 21:11
  • yes, but i should have a model that do all the work? I mean, a model that call the create category method and then creates the object with the returned id of the category. Or should this be done in a controller? the code would be something like $cat->create('blabla'); $prod->create('blabla', $cat->id); the question is where this logic should be implemented, in a new model or in a controller? – gdi Jun 16 '12 at 21:22
  • The parent abstract class should be for covering the products, and categories. Create 2 subclasses, one for products, one for categories. You could pass the category ID or name into one of the parameters when creating the product. The controller will be just for calling the model. – David Jun 16 '12 at 21:28
  • so my thinking is right, i need 3 models, one for product, one for category and one where i just call the two previous one to complete the task? and all this get triggerd by a controller wich holds the parms for the creation of the stuffs? – gdi Jun 16 '12 at 21:35
  • I would have 2 models, you would call them from the controller. So inside the controller you would have `$category->create('dogs');` and after that you would have `$product->add('lab', $categoryID);` or whatever parameters you want to have for adding the product. – David Jun 16 '12 at 21:38
  • But isn't cleaner to do all that logic in a model instead? – gdi Jun 16 '12 at 21:40
  • The logic IS done in the model, you just call them from the controller. Check out the accepted answer response here: http://stackoverflow.com/questions/2621725/mvc-model-view-controller-does-the-view-call-the-model – David Jun 16 '12 at 21:54