goldenline/neo4jphp-ogm

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

dev-master 2012-10-25 09:31 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:26:22 UTC


README

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

该库还基于 Doctrine\Common,并在很大程度上借鉴了优秀的 Doctrine\ORM 设计。

在 MIT 许可下发布。

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

运行测试

  • 必须使用 Composer 加载依赖
  • 必须有一个运行在 localhost:7474 的 neo4j 实例
  • 使用 PHPUnit。

基本用法

为了使用库存储和检索信息,您必须声明您的实体。如果您之前使用过 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 = $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'));

复杂查询

可以使用 Cypher 查询根据任意关系获取节点。查询机制使用查询构建器来简化参数绑定。

$em = $this->get('hirevoice.neo4j.entity_manager');
$john = $repo->findOneByFullName('John Doe');

$list = $em->createCypherQuery()
    ->startWithNode('john', $john)
    ->match('john -[:follow]-> followedBy <-[:follow]- similarInterest')
    ->match('similarInterest -[:follow]-> potentialMatch')
    ->end('potentialMatch', 'count(*)')
    ->order('count(*) DESC')
    ->limit(10)
    ->getList();

// $list is a collection of User objects

就像在 Doctrine 中一样,最好将此查询移动到仓库中。

<?php
namespace Repository;
use HireVoice\Neo4j\Repository as BaseRepository;

class UserRepository extends BaseRepository
{
    function findRecommendations(User $user)
    {
        return $em->createCypherQuery()
            ->startWithNode('user', $user)
            ->match('user -[:follow]-> followedBy <-[:follow]- similarInterest')
            ->match('similarInterest -[:follow]-> potentialMatch')
            ->end('potentialMatch', 'count(*)')
            ->order('count(*) DESC')
            ->limit(10)
            ->getList();
    }
}

实体注解需要修改为指向自定义仓库类

/**
 * @OGM\Entity(repositoryClass="Repository\UserRepository")
 */

通过实体管理器上的 getRepository() 提供适当的仓库。

初始化实体管理器

理想情况下,这将在您的应用程序中通过依赖注入来完成。以下是过程式创建。

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