Yii 1.1: neo4yii

An ActiveRecord like wrapper for the (RESTful) Neo4j graph database


This is an ActiveRecord like wrapper for the RESTful graph database Neo4j. I built this on top of my ActiveResource for Yii extension and although it isn't perfect (missing indexing support and bugs may still lurk) I share my work with the Yii community. This extension is still under development so heads up!

Futher information can be found at Neo4Yii at Github or at the forum thread



  1. Download my ActiveResource extension and add it to your extensions folder (If you haven't done already).

  2. Download the Neo4Yii extension at Github, import EActiveResource and Neo4Yii and configure Neo4Yii this way:



Example: Persons have friends which themselves can also have friends (via a friend relationship). Each friendship can be defined with the property "forYears". e.g.:Old friends know each other for more than 5 years. Here is an example of how to use Neo4Yii in such a case.

class Person extends ENeo4jNode
    public static function model($className=__CLASS__)
        return parent::model($className);
    public function properties()
        return CMap::mergeArray(parent::properties(),array(
    public function rules()
        return array(
    public function traversals()
        return array(
            'fof'=>array(self::HAS_MANY,self::NODE,'out("_FRIEND_").out("_FRIEND_")'),      'oldFriends'=>array(self::HAS_MANY,self::NODE,'outE("_FRIEND_").filter{it.forYears>5}.inV'),
class _FRIEND_ extends ENeo4jRelationship
    public static function model($className = __CLASS__) {
        return parent::model($className);
    public function properties()
        return CMap::mergeArray(parent::properties(),array(
    public function rules()
        return array(
$haensel=new Person;
            $bill=new Person;
            $haensel->addRelationshipTo($bill, '_FRIEND_',array('forYears'=>10));
            $susan=new Person;
            $haensel->addRelationshipTo($susan, '_FRIEND_',array('forYears'=>4));
            $susansFriend=new Person;
            $susan->addRelationshipTo($susansFriend, '_FRIEND_',array('forYears'=>4));
            echo 'Haensels friends:<br>';
            foreach($haensel->friends as $friend)
                echo $friend->name .' '.$friend->surname.'<br>';
            echo 'Haensels old friends:<br>';
            foreach($haensel->oldFriends as $oldFriend)
                echo $oldFriend->name .' '.$oldFriend->surname.'<br>';
            echo 'friends of Haensels friends:<br>';
            foreach($haensel->fof as $fof)
                echo $fof->name .' '.$fof->surname.'<br>';
            Haensels friends:
            Bill Brown
            Susan Scissors
            Haensels old friends:
            Bill Brown
            friends of Haensels friends:
            Susans Friend

Total 10 comments

#16850 report it
Lidiexy at 2014/04/03 04:46pm
I can't work with this awesome Extension

Hi Haensel When i try to view your code example i got this PHP ERROR

Declaration of ENeo4jPropertyContainer::query() should be compatible with EActiveResource::query($route, $method = 'GET', $params = Array, $data = NULL)

Any advice?

Best Regards

#11106 report it
John1983 at 2012/12/17 07:06am
Remove relationship in neo4j

How to remove relationship in neo4j? Please help.

#6988 report it
HLii at 2012/02/17 09:06am

I will open a ticket in GitHub.


#6984 report it
Haensel at 2012/02/17 04:45am
Hi again

Ah, I see your point. This should be quite easy to implement. But could you please open such issues/features add the Github project page so that I can keep track of such ideas?



#6980 report it
HLii at 2012/02/16 09:26pm

Hi, Haensel,

Thank you for add the indexing feature and I am still work on using. I'll post some feedback once I finish my test.

I have one request for your to consider.

Currently the setStartNode and setEndNode takes ENeo4jNode as input. Is it possible to also have them accept integer as input? I am working on something, I've already cached the node ID somewhere in the application. It will be great if I can just pass the node ID to these 2 method and avoid create 2 ENeo4jNode objects.

Please let me know if this is possible.


#6937 report it
Haensel at 2012/02/14 01:40pm

Regarding dataProviders: There is no specific dataProvider class out now for Neo4Yii so if you want to use CListView or CGridView etc. then you should use CArrayDataProvider(YourModel::findAll()). But take care: If you load more than let's say 100 models than this could be slow (also take into account that the simple traversals use "lazy loading").



#6935 report it
Haensel at 2012/02/14 11:56am
Indexing support

I just created a new branch on Github called "index-development" for all the indexing stuff. I hacked this together today so I don't want to merge it with the master branch yet. if you want to test it take a look at the ENeo4jGraphService class to get an idea of how to create indices and how to add/remove property containers. You can also query those indices via three new finder methods called findByExactIndexEntry(), findAllByExactIndexEntry() and findByIndexQuery(). Just look at ENeo4jNode/ENeo4jRelationship to get an idea on how to use them.

I know the documentation sucks, but the PHPdocs should give you a lot of information for now



#6922 report it
HLii at 2012/02/13 04:39pm

Looking forward to it.


#6892 report it
Haensel at 2012/02/11 07:12am
Indexing nodes/relationships

Hi haiyangi,

Not yet, but I plan to add this feature next week or so. It will definitely be part of this extension



#6878 report it
HLii at 2012/02/10 04:10pm

Hi, Haensel,

Thank you for putting this together. I am still learning it. One question came up. Is there a way in Neo4Yii to explicit create index on a Neo4j data store?



Leave a comment

Please to leave your comment.

Create extension
No downloadable files yet