2017 © Pedro Peláez
 

symfony-bundle neo4j-bundle

Neo4j Bundle

image

misteio/neo4j-bundle

Neo4j Bundle

  • Tuesday, August 29, 2017
  • by MisteIO
  • Repository
  • 1 Watchers
  • 0 Stars
  • 379 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 374 % Grown

The README.md

Neo4jBundle

Build Status Code Climate Latest Stable Version codecov, (*1)

Neo4jBundle is a Symfony2/3 Bundle designed for simply use Neo4J 3.x with Doctrine 2.x, (*2)

Installation

Via Composer, (*3)

``` bash $ composer require misteio/neo4j-bundle, (*4)

or in composer.json file
``` bash
"misteio/neo4jbundle-bundle": "dev-master"

Register the bundle in app/AppKernel.php:, (*5)

``` php public function registerBundles() { return array( // ... new Misteio\Neo4jBundle\MisteioNeo4jBundle(), // ... ); }, (*6)


Configuration ------------- Configure your connections and mappings in `app/config/config.yml` : ``` yaml imports: - { resource: parameters.yml } misteio_neo4j: connections: %neo4j.hosts% mappings: %neo4j.mappings%

An then create a file named parameters.yml in app/config, (*7)

``` yaml parameters: neo4j.hosts: graphenedb: host: 'yourHostWithoutScheme' port: yourPort user: username password: yourPassword, (*8)

neo4j.mappings:
  FakeEntity:
    class: '\Misteio\Neo4jBundle\Tests\Entity\FakeEntity'
    transformer: 'neo4j.fakeentity.transformer'
    auto_event: true
    connection: 'graphenedb'
    indexes:
      - 'name'
    # only available for neo4j 3.2 and above  
    composite_indexes: 
      - 'id,name'

Example of entity ```php <?php namespace Name\NameBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class YourEntityClassName { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * Set id * * @param integer $id * @return Id */ public function setId($id) { $this->id = $id; return $this; } /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ protected $name; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return City */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } }

As you can see you have to create a Transformer for your Entities., (*9)

``` php <?php namespace Name\NameBundle\Entity\Transformer\EntityTransformer; use Misteio\Neo4jBundle\Helper\Neo4jHelper; use Name\NameBundle\Entity\YourEntityClassName;, (*10)

class YourTransformerClassName { /** @var Neo4jHelper */ private $neo4jHelper;, (*11)

    /**
     * @param Neo4jHelper $neo4jHelper
     */
    public function setNeo4jHelper(Neo4jHelper $neo4jHelper)
    {
        $this->neo4jHelper = $neo4jHelper;
    }

    /**
     * @param FakeEntity $fake
     * @param $connectionName
     * @return bool
     */
    public function transform(FakeEntity $fake, $connectionName)
    {
        $this->neo4jHelper->getClient($connectionName)->run('CREATE (n:FakeEntity {id :{id}, name:{name}} )', ['id' => $fake->getId(), 'name' => $fake->getName()]);

        return true;
    }

}, (*12)




## Usage If auto_event is set, you have nothing to do for creation, update and deletion of your entities. You can begin to call Neo4j with MisteioNeo4jHelper and Graphaware. Example in a Controller. ```php $client = $this->getContainer()->get('misteio.neo4j.helper')->getClient('graphenedb'); $result = $client->run("Match (n:FakeEntity) RETURN n;"); $nodes = $result->getRecords();

For more information about querying Neo4j, look at GraphAware Neo4j PHP Client, (*13)

If auto_event is not set, you can listen misteio.neo4j.event like this :, (*14)

name.neo4j.subscriber:
    class: Name\NameBundle\Subscriber\Neo4jSubscriber
    tags:
        - { name: kernel.event_listener, event: misteio.neo4j.event, method: onNeo4jEntityAction }

And in your EventListener Class, (*15)

<?php

namespace Name\NameBundle\EventListener;

use Misteio\Neo4jBundle\Event\Neo4jEvent;

class Neo4jListener
{
    /**
     * @param Neo4jEvent $event
     */
    public function onNeo4jEntityAction(Neo4jEvent $event)
    {
        //Action can be persist, update and delete
        $action = $event->getAction();
        //Your Doctrine Entity
        $entity = $event->getEntity();
    }
}

Command for populate

After configuration of your entities, you maybe want make them available on Neo4j. You have to use php app/console misteio:neo4j:populate for Symfony 2 or php bin/console misteio:neo4j:populate for Symfony 3. Differents options are availables :, (*16)

  • --limit=int : Limit of your collection
  • --offset=int : Offset of your collection
  • --type=string : Name of your Object (in our example it's YourEntityClassName)
  • --threads=int : Number of threads you want to use for. If you use it, limit will not be available, and you have to set a batch.
  • --reset : For delete all your Nodes. Can be passed with a type. If type is passed will deleted all node of entity chosen. BE CAREFULL, all your data will be lost in your Neo4j Cluster
  • --reset_index : For reset your indexes. Must be passed with reset
  • --batch=int : Length of collection per threads. Use this only with threads

Security

If you discover a security vulnerability , please email instead of using the issue tracker. All security vulnerabilities will be promptly addressed., (*17)

Standalone Test

How to test

  1. clone repo : $ sudo git clone https://github.com/Misteio/Neo4jBundle.git
  2. go into directory : $ cd Neo4jBundle/
  3. install composer as explained here : https://getcomposer.org/download/
  4. launch composer install : $ ./composer.phar install
  5. launch test : $ ./vendor/bin/phpunit

License

This Bundle is open-sourced software licensed under the MIT license, (*18)

The Versions