6

I have been creating a website using OO for the first time and I'm struggling to find the right way to go. I don't like classes where everything is done in one class so I separated the entity and database class. For example: http://w3cyberlearning.com/php/mysql_oop_product_class.php I find this class not so logical. -why should a product contain a database instance -$product->get_products() insn't logical at all I think.

So I create a separate class according to the following structure:

Table: Products
Fields: productID,productName,categoryID

Table: ProductCategory
Fields: categoryID,category

Classes:
Product
-productID
-productName
-category (object)

DataProduct
-insertProduct($product)
    insert query...
-deleteProduct($product)
    delete query...
-getProduct($id)
    select data from table ... (join between product and category to get all the fields)
    $category = new Category($categoryID,$categoryname)
    $product = new Product($productID,$productName);
    $product->setCategory($category);
    return $product;

ProductCategory
-categoryID
-category

DataProductCategory
...

But now I'm stuck with some questions wether I'm handling it right.

Question 1

To for example, delete a product, I can execute two different scripts, which one should I use?

http://www.mywebsite.com/books/1/
$id = 1 (1 retrieved from the url)
$DataProduct = new DataProduct();
$DataProduct->deleteProduct($id);
OR
$product = new Product();
$product->setId($id);
$DataProduct = new DataProduct();
$DataProduct->deleteProduct($product);

Question 2

When I retrieve a product from the database, I create a new Product Object and I add the category as an object to the Product. Lets say you have an adminpage where you want to add a new product and you have an option list with categorieID ids:

<input type="text" name="productname">
<select name="categories">
<option name="cat1" value="1">
<option name="cat2" value="2">
</select>

If I want to save this product to the database, what is the best way to do this? In the database, the productstable only holds the categoryID offcourse, as an foreign key to the categorytable.

$product = new Product();
$product->setProductName("test");
$product->setCategoryID(1); (and add a variable categoryID in the products class)
$dataProduct = new DataProduct();
$dataProduct->insertProduct($product);

and just get the $product->categoryID and insert it into the database along with the $product->productName

or should I do:

$product = new Product();
$product->setProductName("test");
$dataCategory = new DataCategory();
$dataProduct = new DataProduct();
$category = $dataCategory->getCategory(); (which retrieves the categoryID and name, creates the object and returns it)
$product->setCategory($category);
$dataProduct->insertProduct($product);

and with saving retrieve the id for the category from the object?

Both ways will work, but I'm a little bit stuck on which is the way to go.

randomizer
  • 1,619
  • 3
  • 15
  • 31
  • Belongs at http://codereview.stackexchange.com/ – Wesley van Opdorp Nov 21 '11 at 10:33
  • 1
    The features you're describing are all to do with the way you retrieve stuff from the database. There are existing patterns for doing this - usually known as "object-relational mapping", or ORM. There's an extensive discussion here: http://stackoverflow.com/questions/108699/good-php-orm-library. I'd read up on this - even if you decide not to use it, it will provide context for your own design. – Neville Kuyt Nov 21 '11 at 11:12
  • See also [10 ORM Patterns](http://giorgiosironi.blogspot.com/2009/08/10-orm-patterns-components-of-object.html) for an overview and links to, well, 10 different ORM patterns. – outis Nov 21 '11 at 13:24

1 Answers1

0

Question 1

I advice to use the DataProduct approach. Even though it's more related to the Product, you're already using the DataProduct for simular actions such as inserting and updating. I'd say you need to stick to that same object for deleting as well.

Question 2

Even though my feeling says you should stick to the first method because it's much tidier, the second one is OOP-correct. You're only setting properties to the Product class, so it'd be messy if you'd get the Category object by the ID in your Product class. Instead, you want to set the entire object.


OOP is a difficult theoretical matter but you're doing a great job. I know it's very hard to judge your own OOP because often you look at it from a programming-perspective, rather the OOP-perspective which makes it harder than it already is.

Tim S.
  • 13,597
  • 7
  • 46
  • 72