PHP Mapper Template, From Many to Many Relationships

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.

+4
source share
2 answers

Regarding this particular point:

Will this contradict this pattern, create methods in Product_Mapper called "addCategory", "deleteCategory",

Yes and no. This really has nothing to do with DataMapper. This should be considered as product methods; eg:

 $oProduct->addCategory( $oCategory ); 

Then, when you save the product using datamapper, datamapper will add a link between this product and the category that you added to it.

 $oProductMapper->save( $oProduct ); 
+1
source

I believe that the idea of โ€‹โ€‹the DataMapper template is to convert the database schema to your internal schema. Thus, you can forget about the third database table, which connects products and categories. Therefore, you must create methods such as Category_Mapper::getCategoriesByProductId and Product_Mapper::getProductByCategoryId .

That way, the user uses the Category and Product classes, they use Mappers, and Mapper can use some kind of general class if you want.

I cannot be sure of this, but it seems reasonable.

0
source

Source: https://habr.com/ru/post/1385803/


All Articles