lesha888 / doctrine-key-value-store
一个简单的键值存储抽象层,可以映射到PHP对象,支持多种后端。
v0.2
2015-07-24 14:08 UTC
Requires
Requires (Dev)
- microsoft/windowsazure: dev-master
- riak/riak-client: dev-master
Suggests
- ext-couchbase: to use the Couchbase storage
- aws/aws-sdk-php: to use the DynamoDB storage
- riak/riak-client: to use the Riak storage
This package is auto-updated.
Last update: 2024-09-05 03:54:52 UTC
README
在NoSQL世界中,持久化接口对于许多只有键值存储和一些附加功能的实现来说有些过度。Doctrine键值存储拯救了这种情况。本项目提供了一个更简单、轻量级的API,它围绕键值API来获取/保存对象。
- 单值或多值主键
- 无结构/无模式的值被映射到对象上
- 根据实现,支持嵌入式值/对象
- 不需要复杂的映射,只需在类上添加@Entity,所有属性都会自动映射,除非指定了@Transient。至少有一个属性必须是@Id。不过这取决于底层供应商。
- 属性不必存在于类中,对于缺失的属性会创建公共属性。
- 不支持对其他对象的引用
- ODM/ORM的事件监听器,允许将键值实体及其属性集合作为属性(postLoad、postUpdate、postPersist、postRemove)管理
- 简化后的对象管理接口
- 数据映射器类似于其他Doctrine库,持久化和数据对象是分开的。
- 继承(单存储或多存储)
实现
以下供应商是目标对象
- Microsoft Azure Table(已实现)
- Doctrine\Common\Cache提供者(已实现)
- 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 Table,可以使用以下配置来实例化存储
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);