views:

94

answers:

2

I am trying to design a PHP project's structure. What I am not very sure about is how to implement categories and products relationship. A category has one parent, and can have many children. Product belongs to only one category. Db tables are like this:

products: id product_name category_id

categories id category_name parent_id

I created a class for products, a class for categories. But I do not know what is the best way to relate them in object oriented manner.

Should I have more classes like CategoryManager, if yes what should it look like ?

Anyone has good approach to implement relations in code level, not in db level.

Thanks in advance.

+3  A: 

In my opinion, having methods like Product->getCategory() and Category->getProducts() would be enough.

Ignas R
A: 

I would do it like this

A product can have one Category but a Category can have may Products.

class products 
{
  public $productname;
  public $price;

public function getProduct($id)
{
  return $this->db->fetch("select * from Products where id={$id}");
} 


}


class Category extends Products
{
  public $category;

  public function __construct()
 {
  parent::__construct();
 }

 public function getCategoryProduct($id)
 {
  $this->getProduct($id);
 }

}
streetparade