定制 metarush/data-mapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

metarush/data-mapper

Composer 安装命令:

composer require metarush/data-mapper

包简介

A generic data access layer for basic CRUD operations. Can act as a layer between database and repositories/services.

README 文档

README

A generic data access layer for basic CRUD operations. Can act as a layer between database and repositories/services.

Install

Install via composer as metarush/data-mapper

Usage

Init library

<?php

$builder = (new \MetaRush\DataMapper\Builder)
    ->setDsn('mysql:host=localhost;dbname=example') // PDO DSN
    ->setDbUser('foo')
    ->setDbPass('bar');

$dM = $builder->build();

Create new row

// insert 'foo' in column 'col1' and 'bar' in column 'col2'
$data = [
    'col1' => 'foo',
    'col2' => 'bar'
];
$lastInsertId = $dM->create('table', $data);

Find column

// find value of column 'col2' where 'col1' == 'foo'
$column = $dM->findColumn('table', ['col1' => 'foo'], 'col2');
\print_r($column); // bar

Find row

// find row where column 'col1' == 'foo'
$row = $dM->findOne('table', ['col1' => 'foo']);
\print_r($row);

Find rows

// find all rows
$rows = $dM->findAll('table');
\print_r($rows);

// find rows where column 'col1' = 'foo'
$rows = $dM->findAll('table', ['col1' => 'foo']);
\print_r($rows);

// find rows where column 'col1' = 'foo', order by col1 DESC
$rows = $dM->findAll('table', ['col1' => 'foo'], 'col1 DESC');
\print_r($rows);

// find rows where column 'col1' = 'foo', order by col2 DESC, limit 2, offset 3
$rows = $dM->findAll('table', ['col1' => 'foo'], 'col2 DESC', 2, 3);
\print_r($rows);

// find rows grouped by column 'col1'
$dM->groupBy('col1');
$rows = $dM->findAll('table');
\print_r($rows);

Update rows

$data = ['col1' => 'bar'];
$where = ['col2' => 'foo'];
$dM->update('table', $data, $where);

Delete rows

$where = ['col1' => 'foo'];
$dM->delete('table', $where);

Using $where clause

As per Atlas.Query documentation, if the value of the column given is an array, the condition will be IN (). Given a null value, the condition will be IS NULL. For all other values, the condition will be =. If you pass a key without a value, that key will be used as a raw unescaped condition.

$where = [
    'foo' => ['a', 'b', 'c'],
    'bar' => null,
    'baz' => 'dib',
    'zim = NOW()'
];

The above sample is equivalent to WHERE foo IN (:__1__, :__2__, :__3__) AND bar IS NULL AND baz = :__4__ AND zim = NOW()

Other examples using other WHERE operators:

$where = [
    'foo > 20',
    'bar <= 30',
    'baz BETWEEN 5 AND 10',
    "firstName LIKE 'test%'"
];

Remember, if you pass a key without a value (like these other WHERE operators), they will be unescaped.

Transaction methods

$dM->beginTransaction();
$dM->commit();
$dM->rollBack();

Custom SQL query

You can use prepared statements with placeholders or named parameters

SELECT

$preparedStatement = 'SELECT * FROM table WHERE x = ? AND y = ?';
$bindParams = ['foo', 'bar'];
$fetchStyle = \PDO::FETCH_BOTH; // See https://www.php.net/manual/en/pdostatement.fetch.php for options. Default: \PDO::FETCH_BOTH
$rows = $dM->query($preparedStatement, $bindParams, $fetchStyle);
\print_r($rows);

Single INSERT

$preparedStatement = "INSERT INTO table (firstName, lastName, age) VALUES (?, ?, ?)";
$bindParams = ['Mark', 'Calaway', '18'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams); // returns 1
$lastInsertID = $dM->getLastInsertId();

Multiple INSERT in one statement

$preparedStatement = "INSERT INTO table (firstName, lastName, age) VALUES (?, ?, ?), (?, ?, ?)";
$bindParams = ['Mark', 'Calaway', '18', 'Dwayne', 'Johnson', '17'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams); // returns 2
$lastInsertID = $dM->getLastInsertId();

UPDATE

$preparedStatement = "UPDATE table SET age = ? WHERE lastName = 'Doe'";
$bindParams = ['18'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams);

DELETE

$preparedStatement = "DELETE FROM table WHERE lastName = ?";
$bindParams = ['Doe'];
$numberOfAffectedRows = $dM->exec($preparedStatement, $bindParams);

Optional config/builder methods

->setStripMissingColumns(true);

If set totrue,create()and update() methods will strip missing columns in their $data parameter.

->setTablesDefinition(array $tablesDefinition);

Required when using setStripMissingColumns(true)

Example parameter for $tablesDefinition:

$tablesDefinition = [
    'UsersTable' => [ // table name
        'id', 'firstName', 'lastName' // column names
    ],
    'PostsTable' => [ // table name
        'id', 'subject', 'message' // columns names
    ]
];

Current adapters

  • PDO (via Atlas.Query)

metarush/data-mapper 适用场景与选型建议

metarush/data-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 324 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「data mapper」 「pdo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 metarush/data-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 metarush/data-mapper 我们能提供哪些服务?
定制开发 / 二次开发

基于 metarush/data-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 324
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 27
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-21