hirevoice/neo4jphp-ogm

Neo4j 图形数据库的 Doctrine2 风格实体映射器

v0.6.0 2014-04-30 23:05 UTC

This package is not auto-updated.

Last update: 2024-09-14 11:45:41 UTC


README

Build Status

关于

Neo4j PHP 对象图形映射器是构建在 everyman/neo4jphp 之上的对象管理层。它允许通过 REST 连接器在 Neo4j 图形数据库中操作数据。

该库还基于 Doctrine\Common,并从出色的 Doctrine\ORM 设计中借鉴了许多内容。

在 MIT 许可下发布。

由 Louis-Philippe Huberdeau 为 HireVoice Inc. 创建,该库从项目的代码库中提取出来,成为一个独立的开源项目。欢迎使用、评论和参与。

基本用法

为了使用库来存储和检索信息,您必须声明您的实体。如果您之前使用过 Doctrine2,这个过程非常相似。

<?php
namespace Entity;

use HireVoice\Neo4j\Annotation as OGM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * All entity classes must be declared as such.
 *
 * @OGM\Entity
 */
class User
{
    /**
     * The internal node ID from Neo4j must be stored. Thus an Auto field is required
     * @OGM\Auto
     */
    protected $id;

    /**
     * @OGM\Property
     * @OGM\Index
     */
    protected $fullName;

    /**
     * @OGM\ManyToMany
     */
    protected $follows;

    /**
     * @OGM\ManyToOne
     */
    protected $referrer;

    function __construct()
    {
        $this->friends = new ArrayCollection;
    }

    /* Add your accessors here */
}

将实体存储到图形数据库中

// Let's assume the entity manager is initialized. More on this later.
$em = $this->get('hirevoice.neo4j.entity_manager');
$repo = $em->getRepository('Entity\\User');

// The repository uses magic functions to search in indexed fields
$john = $repo->findOneByFullName('John Doe');

$jane = new User;
$jane->setFullName('Jane Doe');

$jane->addFollow($john);

$em->persist($jane);
$em->flush(); // Stores both Jane and John, along with the new relation

$em->remove($john);
$em->flush(); // Removes John and the relation to Jane

从数据库中检索实体

$em = $this->get('hirevoice.neo4j.entity_manager');
$repository = $em->getRepository('Entity\\User');

// Find a User by a specific field
$user = $repository->findOneByFullName('superman'); // Returns a User object

// Find some users by a specific field
$usersFromFrance = $repository->findByCountry('FR'); // Returns a collection of User object

// Find one User with more than one criteria
$nonActiveWithSuchEmail = $repository->findOneBy(array('status' => 'idle', 'email' => 'superman@chucknorris.com'));

// Find Multiple Users with more than one criteria
$activeUsersFromFrance = $repository->findBy(array('status' => 'active', 'country' => 'FR'));

初始化 EntityManager

理想情况下,这应该在您的应用程序中通过 DependencyInjection 完成。以下是程序性创建的方法。

$em = new HireVoice\Neo4j\EntityManager(array(
    // 'transport' => 'curl', // or 'stream'
    // 'host' => 'localhost',
    // 'port' => 7474,
    // 'username' => null,
    // 'password' => null,
    // 'proxy_dir' => '/tmp',
    // 'debug' => true, // Force proxy regeneration on each request
    // 'annotation_reader' => ... // Should be a cached instance of the doctrine annotation reader in production
));

完整文档

要获取完整文档,请参阅 doc 目录