Using a matching pattern is best practice for defining classes for many, many relationships.
For example, let's say we have tables for products and categories and Product_Categories
Here are some basic skeletons for products and categories. Each of them has a class of objects, each of which has a Mapper.
Product Object and Product Mapper:
class Product { public $product_id; public $name; public function __construct($product_id = false, $name = false) { $this->product_id = $product_id; $this->name = $name; } } class Product_Mapper { private $_db; public function __construct($_db) {} public function getProducts() {} public function getProductById($product_id) {} public function insert(Product $product) {} public function save(Product $product) {} public function update(Product $product) {} public function delete(Product $product) {} }
Category Object and Category Mapper
class Category { public $category_id; public $name; public function __construct($category_id = false, $name = false) { $this->category_id = $category_id; $this->name = $name; } } class Category_Mapper { private $_db; public function __construct($_db) {} public function getCategories() {} public function getCategoryById($product_id) {} public function insert(Category $category) {} public function save(Category $category) {} public function update(Category $category) {} public function delete(Category $category) {} }
What is missing here is the ability to add products to categories and update / delete / select products from categories, etc.
Will you create methods against this template against Product_Mapper called addCategory , deleteCategory , getProductsByCategoryId or will you create a new object using mapper called Product_Categories that will handle these functions?
Really appreciate any feedback. I could see that any of them fit, but without creating a new class, I could also see how the product class swells as new relationships are created.
source share