douchi1945 2011-12-11 22:07
浏览 6

PHP Mapper Pattern,多对多关系

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.

  • 写回答

2条回答 默认 最新

  • dougan6982 2011-12-21 07:01
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?