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);