PHP Mapper Pattern, many to many relationships

When using the mapper pattern, what is the best practice for defining classes for many to 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 has an object class and each has a Mapper.

Product Object & 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's missing here is the ability to add products to categories and update/delete/select products from categories, etc.

Would it be going against this pattern, to create a methods within Product_Mapper called addCategory , deleteCategory , getProductsByCategoryId or would you create a new object with mapper called Product_Categories that would handle those functions?

Really appreciate any feedback. I could see either way being suitable, but without creating a new class, I could also see the product class becoming bloated as new relationships are built.


Regarding this particular point:

Would it be going against this pattern, to create a methods within Product_Mapper called "addCategory", "deleteCategory",

Yes, and no. It really has nothing to do with DataMapper. Those should be considered Product methods; eg:

$oProduct->addCategory( $oCategory );

Then, when you save the product with the datamapper, the datamapper will add the relationship between that product and that category you added to it.

$oProductMapper->save( $oProduct );

I believe the idea of the DataMapper pattern is to convert the schema of DB to your inner schema. So you can forget about the third database table that connects products and categories. Thereby you should create methods like Category_Mapper::getCategoriesByProductId and Product_Mapper::getProductByCategoryId .

So, user uses Category and Product classes, they use Mappers, and Mapper can use some common class if you want.

I can't be sure about all this but it seems to be reasonable.

链接地址: http://www.djcxy.com/p/56252.html

上一篇: 通过数据映射器处理集合模式中的项目

下一篇: PHP Mapper模式,多对多的关系