solleer / maphper-loader

此包最新版本(v3.1)没有可用的许可信息。

v3.1 2019-05-19 03:06 UTC

This package is auto-updated.

Last update: 2024-09-17 00:43:23 UTC


README

创建 Maphper 实例的简单方法

依赖项

此库依赖于 Dice 和 Maphper,这两个都可以在 Level-2 找到。

目的

此项目开始是为了使创建 Level-2 中 Maphper 类的实例变得更容易,而配置非常少。每个 Loader 只需要一个文件传递给它,然后你可以调用 getMaphper 方法来获取所需的 Maphper。

使用

让我们从 Maphper 的 README 中的第一个示例开始

$pdo = new PDO('mysql:dbname=maphpertest;host=127.0.0.1', 'username', 'password');
$blogSource = new \Maphper\DataSource\Database($pdo, 'blog', 'id');
$blogs = new \Maphper\Maphper($blogSource);

这可以改为一个名为 config.json 的文件,其中包含 Maphper 配置信息

{
    "blogs" : {
        "type" : "database",
        "table" : "blog",
        "primaryKey" : "id"
    }
}

然后 PHP 变为

$loader = new \MaphperLoader\Json("config.json", new \Dice\Dice());
$blogs = $loader->getMaphper("blogs");

MaphperLoader 使用 dice 处理依赖项,如 PDO。你只需要为 PDO 添加一个规则,然后就可以设置好了。

关系

MaphperLoader 支持所有 Maphper 关系,包括:一对一、多对一和多对多。

一对一

以下是一对一关系的示例 JSON

{
    "blog" : {
        "type" : "database",
        "table" : "blog",
        "primaryKey" : "id",
        "relations" : [
            {
                "name" : "author",
                "to" : "author",
                "type" : "one",
                "localKey" : "authorId",
                "foreignKey" : "id"
            }
        ]
    },
    "author" : {
        "type" : "database",
        "table" : "author",
        "primaryKey" : "id"
    }
}

一对一关系具有以下属性

  • name - 访问关系时的名称
  • to - 连接到的 Maphper
  • type - 关系类型
  • localKey - 当前 Maphper 中链接的键
  • foreignKey - 连接到其他 Maphper 的键

多对一

{
    "blog" : {
        "type" : "database",
        "table" : "blog",
        "primaryKey" : "id"
    },
    "author" : {
        "type" : "database",
        "table" : "author",
        "primaryKey" : "id",
        "relations" : [
            {
                "name" : "blogs",
                "to" : "blog",
                "type" : "many",
                "localKey" : "id",
                "foreignKey" : "authorId"
            }
        ]
    }
}

多对一关系与一对一关系具有相同的属性,除了 type 被设置为 many

多对多

{
    "actors" : {
        "type" : "database",
        "table" : "actor",
        "primaryKey" : "aid",
        "relations" : [
            {
                "name" : "movies",
                "to" : "movies",
                "type" : "ManyMany",
                "intermediate" : "cast",
                "intermediateKey" : "movieId",
                "foreignKey" : "mid"
            }
        ]
    },
    "movies" : {
        "type" : "database",
        "table" : "movie",
        "primaryKey" : "mid",
        "relations" : [
            {
                "name" : "actors",
                "to" : "actors",
                "type" : "ManyMany",
                "intermediate" : "cast",
                "intermediateKey" : "actorId",
                "foreignKey" : "aid"
            }
        ]
    },
    "cast" : {
        "type" : "database",
        "table" : "cast",
        "primaryKey" : ["movieId", "actorId"]
    }
}

多对多关系必须有一个中间 Maphper 来连接两个 Maphper。连接的两个 Maphper 也必须定义 ManyMany 关系。在上面的示例中,这是 cast Maphper。

多对多关系具有以下属性

  • name - 访问关系时的名称
  • to - 连接到的 Maphper
  • type - 关系类型,在本例中为 "ManyMany"
  • intermediate - 中间 Maphper
  • intermediateKey - 连接到其他 Maphper 的中间 Maphper 列表的键
  • foreignKey - 必须是另一个 Maphper 的主键