mehira/simple-mapper

PostgreSQL 映射器

v1.0.4 2021-04-10 15:18 UTC

This package is auto-updated.

Last update: 2024-09-10 22:35:23 UTC


README

/!\ 仅作原型。请勿在生产环境中使用

安装

composer require "mehira/simple-mapper"

示例

<?php

include __DIR__.'/vendor/autoload.php';

$sql = "select c from customer c WHERE id = $( id);";

$serializer = new Serializer(); // ...

$dbal = new \SimpleMapper\Dbal($serializer, 'pgsql:host=localhost;port=5432;dbname=testdb;user=theuser;password=secretPass');

$stmt = $dbal->prepare($sql);

$stmt->hydrate('c', Customer::class)
    ->setParameter('id', 1)
    ->execute();

$customer = $stmt->fetchColumn();

完整示例

<?php
// ...
$dbal = new \SimpleMapper\Dbal($serializer, 'pgsql:host=localhost;port=5432;dbname=testdb;user=theuser;password=secretPass');

$stm = $dbal->prepare("select 12 as i, c from custormer c where name ILIKE $(nameClient) ");

$stm->hydrate('i', 'int')
    ->hydrate('b', 'bool')
    ->hydrate('c', Customer::class) // here the array key name is not defined, It will be 'c'
    ->hydrate('c', Customer::class, 'theCustomer') // here the array key name is defined and will be 'theCustomer'
    ->hydrate(['col1', 'col2', 'col3'], Customer::class) // will hydrate using several columns, The key name here is c1.
    ->hydrate(['col1', 'col2', 'col3'], Customer::class) // will hydrate using several columns, The key name here is c1.
    ->hydrate(['col1', 'col2', 'col3'], Customer::class, 'cus') // will hydrate using several columns, The name here is cus

    ->setParameter('id', 123)
    ->setParameter('nameClient', 'test', 'varchar') // we can force the type. In this case the final value will be 'test'::varchar

    ->setParameters(['id' => 123, ['name' => 'ref', 'value' => 'ABC', 'type' => 'varchar']])

    ->extractParametersFrom($object) // for each placeholder, it will search value from variable name from the object
    ->extractParametersFrom($object, ['name']) // for each placeholder in the array, it will search value from variable name from the object
    ->extractParametersFrom($object, ['name'], 'cus_') // for each placeholder in the array, it will search value from variable name that started with cus_ from the object

    ->execute();

$rows = $stm->fetch(); // will return a generator
$rows = $stm->fetchAll(); // will return all lines as array assoc
$rows = $stm->fetchColumn(); // will return the first column
$rows = $stm->fetchColumn(0); // will return the first column
$rows = $stm->fetchColumn(1); // will return the second column
$rows = $stm->fetchColumn('c'); // will return the second column

$sql = $stm->getQuery(); // will return the final sql query with array of parameters

事务示例

<?php 
// ...
    $dbal = new \SimpleMapper\Dbal($serializer, 'pgsql:host=localhost;port=5432;dbname=testdb;user=theuser;password=secretPass');
    
    $dbal->beginTransaction();
    $stmt = $dbal->prepare("INSERT INTO customer VALUES (99, 'Alain', NOW())");
    $stmt->execute();
    
    $dbal->addSavePoint('beforeUpdate');
    
    $stmt = $dbal->prepare("UPDATE customer SET name = 'bla bla' WHERE id = 1");
    $stmt->execute();
    
    $stmt = $dbal->prepare("select name from customer WHERE id = 1");
    $stmt->execute();
    $name = $stmt->fetchColumn();
    
    assert('bla bla' == $name);
    
    $dbal->rollBack('beforeUpdate');
    
    $stmt = $dbal->prepare("select name from customer WHERE id = 1");
    $stmt->execute();
    $name = $stmt->fetchColumn();
    
    assert('Albert' == $name);
    
    $dbal->commit();
    
    $stmt = $dbal->prepare("select name from customer WHERE id = 99");
    $stmt->execute();
    $name = $stmt->fetchColumn();
    
    assert('Alain' == $name);

集成到 Symfony

Simple-Mapper 依赖于 Symfony Serializer。

  • 首先,将其添加到您的 Symfony 项目中
composer require serializer
  • .env 文件中创建环境变量
DATABASE_DSN=pgsql:host=localhost;port=5432;dbname=testdb;user=theuser;password=secretPass
  • services.yml 中创建一个新服务
    SimpleMapper\Dbal:
        arguments:
            $dsn: "%env(DATABASE_DSN)%"

您可以创建一个针对特定表的存储库

<?php

namespace App\Repository\Communication;

use SimpleMapper\Repository\AbstractRepository;

class CustomerRepository extends AbstractRepository
{
    protected function getBaseClass(): string
    {
        return Customer::class;
    }

    protected function getTableName(): string
    {
        return "sales.customer";
    }
}

存储库已准备好在您的服务中使用

<?php
// ...
    /**
     * @param CustomerRepository $repository
     * @return Customer[]
     */
    public function getAllCustomers(CustomerRepository $repository): array
    {    
        return $repository->findAll();
    }

    /**
     * @param CustomerRepository $repository
     * @return Customer[]
     */
    public function getFrenchCustomers(CustomerRepository $repository): array
    {    
        return $repository->find(['country' => 'FR']);
    }

    /**
     * @param CustomerRepository $repository
     * @return Customer
     */
    public function getOneCustomer(CustomerRepository $repository): Customer
    {    
        return $repository->findFirst(['is_master' => true]);
    }