Yii Framework Forum: [EXTENSION] Neo4Yii - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[EXTENSION] Neo4Yii An ActiveRecord like wrapper for Neo4j Rate Topic: -----

#1 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 02 January 2012 - 02:10 PM

Neo4Yii is a wrapper for the open source graph database Neo4j using its RESTful interface. Many problematic SQL queries like finding friends of friends are easily done with a graph database. This extension is still under development so heads up using it, although it should be quite stable right now.

Here is an 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(
            'name'=>array('type'=>'string'),
            'surname'=>array('type'=>'string'),
            'age'=>array('type'=>'integer'),
            'gender'=>array('type'=>'string'),
        ));
    }
    
    public function rules()
    {
        return array(
            array('name,surname,age,gender','safe'),
            array('age','numerical','integerOnly'=>true),
            array('name','required')
        );
    }
    
    public function traversals()
    {
        return array(
            'friends'=>array(self::HAS_MANY,self::NODE,'out("_FRIEND_")'),
            '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(
            'forYears'=>array('type'=>'integer'),
        ));
    }
    
    public function rules()
    {
        return array(
            array('forYears','safe'),
            array('forYears','numerical'),
        );
    }
}


			$haensel=new Person;
            
            $haensel->attributes=array(
                'name'=>'Johannes',
                'surname'=>'Bauer',
                'age'=>29,
                'gender'=>'m'
            );
            $haensel->save();
                        
            $bill=new Person;
            $bill->attributes=array(
                'name'=>'Bill',
                'surname'=>'Brown',
                'age'=>26,
                'gender'=>'m'
            );
            $bill->save();
            
            $haensel->addRelationshipTo($bill, '_FRIEND_',array('forYears'=>10));
            
            $susan=new Person;
            $susan->attributes=array(
                'name'=>'Susan',
                'surname'=>'Scissors',
                'age'=>31,
                'gender'=>'f'
            );
            $susan->save();
            
            $haensel->addRelationshipTo($susan, '_FRIEND_',array('forYears'=>4));
            
            $susansFriend=new Person;
            $susansFriend->attributes=array(
                'name'=>'Susans',
                'surname'=>'Friend',
                'age'=>40,
                'gender'=>'m'
            );
            $susansFriend->save();
            
            $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
            */


Enjoy and have fun! If you find any bugs please post them to Github so that others can benefit from it
0

#2 User is offline   Fel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-January 12

Posted 07 January 2012 - 12:49 AM

I cant make it work. Please give me more detailed installation or requirements...
Error:
  • Only variables should be passed by reference : protected\extensions\Neo4jYii\ENeo4jPropertyContainer.php(134)

$haensel=new Person;
$haensel->attributes=array(
    'name'=>'Johannes',
    'surname'=>'Bauer',
    'age'=>29,
    'gender'=>'m'
);
$haensel->save();


On Yii 1.1.9,
0

#3 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 07 January 2012 - 09:24 AM

Hi,

Just did a fresh install with Yii 1.1.9 and downloaded both extensions according to the README and it works for me. (Btw. Fixed some typos in the README regarding the folder name and the traversals, so check them out).

I assume it is a PHP versioning issue. I use PHP 5.3.5 (MAMP). What's your PHP version?
0

#4 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 07 January 2012 - 11:29 AM

Hi again,

Looking into this issue I think I found the problem and fixed it. Some PHP versions seem to have a problem with this code:

public function getId()
    {
        $uri=$this->self;
        return end(explode('/',$uri));
    }


as the explode function itself is passed. I changed this to the following adding a temp variable

public function getId()
    {
        $uri=$this->self;
        return end($explodedUri=explode('/',$uri));
    }

0

#5 User is offline   Fel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 05-January 12

Posted 07 January 2012 - 11:16 PM

i updated my PHP version and it works. Thank you... Neo4j is great graph db...
0

#6 User is offline   John1983 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 19-October 12

Posted 17 December 2012 - 07:03 AM

How to remove relationship in yii neo4j? Please help.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users