atijust/mappa

一个简单的PDO对象映射器

dev-master 2015-09-15 15:29 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:49:05 UTC


README

Build Status Scrutinizer Code Quality Code Coverage License

Mappa是一个简单的PDO对象映射器。

$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS, [Mappa\Statement::class, [new Mappa\Hydrator()]]);

$stmt = $pdo->prepare("SELECT *, books.name || ' - ' || categories.name AS title FROM books JOIN categories ON categories.id = books.category_id WHERE books.id = ?");

echo get_class($stmt);
// Mappa\Statement

$stmt->execute([1]);

var_export($stmt->hydrate([Book::class, Category::class]));
// array (
//   'books' =>
//   Book::__set_state(array(
//      'id' => '1',
//      'name' => 'B01',
//      'category_id' => '1',
//   )),
//   'categories' =>
//   Category::__set_state(array(
//      'id' => '1',
//      'name' => 'C01',
//   )),
//   '' =>
//   stdClass::__set_state(array(
//      'title' => 'B01 - C01',
//   )),
// )

Mappa适用于PHP 5.5或更高版本。

待办事项

  • 改进错误处理
  • 向实体构造函数传递自定义参数