Doctrine Key Value Stores

简单旧PHP对象映射的键值抽象。(Abstraction for Key-Value to Plain Old PHP Object mapping.)

Github stars Tracking Chart

Doctrine Key Value Stores

对于NoSQL世界中的许多实现而言,持久性(Persistence)接口相当过度,这些实现只是键值存储,顶部还有一些额外的功能。 Doctrine Key Value Store到救援。 这个项目提供了一个更简单的轻量级API,它以一个键值API为中心来获取/保存对象。

  • 单值或多值主键
  • 映射到对象的非结构化/无模式值
  • 根据实现支持嵌入的值/对象
  • 不需要复杂的映射,只需将@Entity放在类上,除非给出@Transient,否则所有属性都会自动映射。至少有一个属性必须是@Id。尽管取决于底层供应商。
  • 属性不必在类中存在,公共属性是为缺少的属性创建的。
  • 不支持引用其他对象
  • ODM/ORM的EventListener允许管理键值实体及其集合作为属性(postLoad,postUpdate,postPersist,postRemove)
  • 删除对象管理器界面
  • Data-mapper与其他Doctrine库以及持久性和数据对象是分开的。
  • 继承(单个或多个存储)

实现

以下供应商是针对性的:

  • Microsoft Azure Table(已实施)
  • Doctrine\Common\Cache provider(已实施)
  • RDBMS(已实施)
  • Couchbase(已实施)
  • Amazon DynamoDB(已实施)
  • CouchDB(已实施)
  • Cassandra
  • MongoDB(已实施)
  • Riak(已实施)
  • Redis(已实施)

我们很高兴接受任何厂商对驱动的贡献。

示例

假设我们根据广告系列ID和收件人跟踪电子邮件广告系列。

<?php
use Doctrine\KeyValueStore\Mapping\Annotations as KeyValue;

/**
 * @KeyValue\Entity(storageName="responses")
 */
class Response
{
    const RECEIVE = 0;
    const OPEN = 10;
    const CLICK = 20;
    const ACTION = 30;

    /** @KeyValue\Id */
    private $campaign;
    /** @KeyValue\Id */
    private $recipient;
    private $status;
    private $date;

    public function __construct($campaign, $recipient, $status)
    {
        $this->campaign = $campaign;
        $this->recipient = $recipient;
        $this->status = $status;
    }
}

创建

<?php
$response = new Response("1234", "kontakt@beberlei.de", Response::RECEIVE);

$entityManager->persist($response);
//.... persists as much as you can :-)

$entityManager->flush();

读取

<?php
$response = $entityManager->find("Response",array("campaign" => "1234","recipient" => "kontakt@beberlei.de"));

更新

与创建相同,只是重复使用相同的ID。

删除

<?php
$response = $entityManager->find("Response",array("1234","kontakt@beberlei.de"));
$entityManager->remove($response);
$entityManager->flush();

配置

还没有简化创建过程的工厂,这里是使用Doctrine Cache后端实例化KeyValue EntityManager所需的完整代码:

<?php
use Doctrine\KeyValueStore\EntityManager;
use Doctrine\KeyValueStore\Configuration;
use Doctrine\KeyValueStore\Mapping\AnnotationDriver;
use Doctrine\KeyValueStore\Storage\DoctrineCacheStorage;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Annotations\AnnotationReader;

$cache = new ArrayCache;
$storage = new DoctrineCacheStorage($cache);

$reader = new AnnotationReader();
$metadata = new AnnotationDriver($reader);
$config = new Configuration();
$config->setMappingDriverImpl($metadata);
$config->setMetadataCache($cache);

$entityManager = new EntityManager($storage, $config);

如果您想使用WindowsAzure表,您可以使用以下配置 实例化存储:

use Doctrine\KeyValueStore\Storage\AzureSdkTableStorage;
use WindowsAzure\Common\ServicesBuilder;

$connectionString = ""; // Windows Azure Connection string
$builder = ServicesBuilder::getInstance();
$client = $builder->createTableService($connectionString);

$storage = new AzureSdkTableStorage($client);

如果您想使用Doctrine DBAL作为后端:

$params = array();
$tableName = "storage";
$idColumnName = "id";
$dataColumnName = "serialized_data";

$conn = DriverManager::getConnection($params);
$storage = new DBALStorage($conn, $tableName, $idColumnName, $dataColumnName);

Overview

Name With Ownerdoctrine/KeyValueStore
Primary LanguagePHP
Program languagePHP (Language Count: 2)
PlatformBSD, Linux, Mac, Windows
License:MIT License
Release Count4
Last Release Namev0.4.0 (Posted on )
First Release Namev0.1 (Posted on 2012-05-08 22:31:55)
Created At2012-02-13 16:08:13
Pushed At2020-05-18 00:09:45
Last Commit At2019-11-15 19:45:19
Stargazers Count201
Watchers Count18
Fork Count59
Commits Count244
Has Issues Enabled
Issues Count33
Issue Open Count15
Pull Requests Count54
Pull Requests Open Count6
Pull Requests Close Count8
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Doctrine Key Value Stores

Build Status

The Persistence interfaces are rather overkill for many implementations in the NoSQL world that are only key-value stores with some additional features on top. Doctrine Key Value Store to the rescue. This project offers a much simpler lightweight API that is centered on a key-value API to fetch/save objects.

  • Single- or multi-value primary keys
  • Unstructured/schema-less values that are mapped onto objects
  • Depending on the implementation embedded values/objects are supported
  • No complex mapping necessary, just put @Entity on the class and all properties are automatically mapped unless @Transient is given. At least one property has to be @Id. Depends on the underlying vendor though.
  • Properties dont have to exist on the class, public properties are created for missing ones.
  • No support for references to other objects
  • EventListener for ODM/ORM that allows to manage key-value entities and collections of them as properties (postLoad, postUpdate, postPersist, postRemove)
  • Stripped down Object Manager Interface
  • Data-mapper as any other Doctrine library and persistence and data-objects are separated.
  • Inheritance (Single- or Multiple-Storage)

Implementations

Following vendors are targeted:

  • Microsoft Azure Table (Implemented)
  • Doctrine\Common\Cache provider (Implemented)
  • RDBMS (Implemented)
  • Couchbase (Implemented)
  • Amazon DynamoDB (Implemented)
  • CouchDB (Implemented)
  • Cassandra
  • MongoDB (Implemented)
  • Riak (Implemented)
  • Redis (Implemented)

We happily accept contributions for any of the drivers.

Example

Suppose we track e-mail campaigns based on campaign id and recipients.

<?php
use Doctrine\KeyValueStore\Mapping\Annotations as KeyValue;

/**
 * @KeyValue\Entity(storageName="responses")
 */
class Response
{
    const RECEIVE = 0;
    const OPEN = 10;
    const CLICK = 20;
    const ACTION = 30;

    /** @KeyValue\Id */
    private $campaign;
    /** @KeyValue\Id */
    private $recipient;
    private $status;
    private $date;

    public function __construct($campaign, $recipient, $status)
    {
        $this->campaign = $campaign;
        $this->recipient = $recipient;
        $this->status = $status;
    }
}

Create

<?php
$response = new Response("1234", "kontakt@beberlei.de", Response::RECEIVE);

$entityManager->persist($response);
//.... persists as much as you can :-)

$entityManager->flush();

Read

<?php
$response = $entityManager->find("Response",array("campaign" => "1234","recipient" => "kontakt@beberlei.de"));

Update

same as create, just reuse the same id.

Delete

<?php
$response = $entityManager->find("Response",array("1234","kontakt@beberlei.de"));
$entityManager->remove($response);
$entityManager->flush();

Configuration

There is no factory yet that simplifies the creation process, here is the
full code necessary to instantiate a KeyValue EntityManager with a Doctrine
Cache backend:

<?php
use Doctrine\KeyValueStore\EntityManager;
use Doctrine\KeyValueStore\Configuration;
use Doctrine\KeyValueStore\Mapping\AnnotationDriver;
use Doctrine\KeyValueStore\Storage\DoctrineCacheStorage;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Annotations\AnnotationReader;

$cache = new ArrayCache;
$storage = new DoctrineCacheStorage($cache);

$reader = new AnnotationReader();
$metadata = new AnnotationDriver($reader);
$config = new Configuration();
$config->setMappingDriverImpl($metadata);
$config->setMetadataCache($cache);

$entityManager = new EntityManager($storage, $config);

If you want to use WindowsAzure Table you can use the following configuration
to instantiate the storage:

use Doctrine\KeyValueStore\Storage\AzureSdkTableStorage;
use WindowsAzure\Common\ServicesBuilder;

$connectionString = ""; // Windows Azure Connection string
$builder = ServicesBuilder::getInstance();
$client = $builder->createTableService($connectionString);

$storage = new AzureSdkTableStorage($client);

If you want to use Doctrine DBAL as backend:

$params = array();
$tableName = "storage";
$idColumnName = "id";
$dataColumnName = "serialized_data";

$conn = DriverManager::getConnection($params);
$storage = new DBALStorage($conn, $tableName, $idColumnName, $dataColumnName);
To the top