delphi/php-orm

用于与图数据库交互的ORM

dev-main 2021-08-22 15:16 UTC

This package is auto-updated.

Last update: 2024-09-22 21:57:25 UTC


README

处理GraphDB中对象持久化的基本结构

⚠ 目前尚不支持任何类型的数据提取。现在它实际上是只写的。

用法

设置

// Create a new entity manager, but first we need...

// ... a DB client object (See delphi/client-laudis for an *actual* implementation) ...
$client = new class implements \delphi\ORM\Client\ClientInterface {};

// ... a QueryBuilder (with its own dependencies) ...
$annotationReader = new \Doctrine\Common\Annotations\AnnotationReader();
$annotationDriver = new \delphi\ORM\Driver\AnnotationDriver($annotationReader);
$queryBuilder = new \delphi\ORM\QueryBuilder($annotationDriver);

$em = new \delphi\ORM\EntityManager($client, $queryBuilder);

配置

namespace App;
use delphi\ORM\Annotation as OGM;

/**
 * @OGM\Entity(label="Foo") 
 */
class Foo {
    /**
     * @OGM\Property(name="name") 
     */
    public string $name = '';
    
    /**
     * @OGM\Relationship(type="CHILD_OF", targetEntity="\App\Bar") 
     */
    public Bar $parentBar;
}
  • 将对象设置为 Entity(label="LabelVal")
    • label 是在图数据库中使用的标签字符串
  • 将单个属性设置为 Property(name="propertyName", unique=true)
    • name 是要赋予属性的名称
    • unique 是一个可选标志,表示特定属性是否在具有此标签的所有实体中是唯一的。默认为 false
  • 将关系设置为 Relationship(type="RELATIONSHIP_TYPE", targetEntity="\App\Bar", multiple=false)
    • type 是在图数据库内部边(关系)中使用的名称
    • targetEntity 是关系另一端的PHP类名
    • multiple 是一个可选标志,表示关系是1对1还是1对多

否则,实体是普通的PHP对象,你可以按需使用。

如果你希望属性不是公共的,你可以为单个属性添加getter方法。请参阅 \delphi\ORM\Util\PropertyGetter

执行

// Create a new entity...
$entity = new Foo();
$entity->name = 'Nombre';
$entity->parentBar = new Bar();

// ... and set it to be persisted
$em->persist($entity);

// There won't be any DB interactions performed until...

// ...actually save the entity off to the DB
$em->flush();

这将导致2个节点之间有1条边。

+---------------+
|      Foo      |
| name="Nombre" |
+---------------+
  |
  | CHILD_OF
  v
+---------------+
|      Bar      |
+---------------+