views:

232

answers:

5

I want to use a graph database using php. Can you point out some resources on where to get started? Is there any example code / tutorial out there? Or are there any other methods of storing data that relate to each other in totally random/abstract situations?

-

Very abstract example of the relations needed: John relates to Mary, both relate to School, John is Tall, Mary is Short, John has Blue Eyes, Mary has Green Eyes, query I want is which people are related to 'Short people that have Green Eyes and go to School' -> answer John

-

Another example:

    TrackA -> ArtistA
           -> ArtistB

           -> AlbumA -----> [ label ]
           -> AlbumB -----> [   A   ]

           -> TrackA:Remix
           -> Genre:House

           -> [ Album ] -----> [ label ]
   TrackB  -> [   C   ]        [   B   ]

Example queries:

Which Genre is TrackB closer to? answer: House - because it's related to Album C, which is related to TrackA and is related to Genre:House

Get all Genre:House related albums of Label A : result: AlbumA, AlbumB - because they both have TrackA which is related to Genre:House

-

It is possible in MySQL but it would require a fixed set of attributes/columns for each item and a complex non-flexible query, instead I need every attribute to be an item by itself and instead of 'belonging' to something, to be 'related' to something.

+1  A: 

Sounds to me a little bit like a "typical" Prolog problem... which is a quite different programming language than PHP. But perhaps you could work with popen.

Or you define an SQL table with columns [ id, predicate, atom1, atom2 ], to store the truthness of "Mary has Green Eyes": predicate = "has", atom1 = "Mary", atom2 = "Green Eyes".

Now you could join and filter the predicates and attributes with the SQL of your choice.

osti
I think you would need a row for every kind of relationship which can be quite a few rows if you handle a lot of data such as a music or movie database with tags and authors, artists, genre and more relationships?
stagas
+4  A: 

There's some work going on to make the Neo4j graph database available from PHP, see this wiki page for more information! Regarding how to model your domain as a graph, the user mailing list tends to be pretty awesome.

Update: there's now a short getting started blog post for a PHP neo4j REST client.

nawroth
neo4j REST client query quite slow if compare to mysql
conandor
+1  A: 

It looks like a semantic web problem. So you have to figure out how you can use PHP and the semantic web together. Maybe this link http://bnode.org/blog/2009/05/25/back-from-new-york-semantic-web-for-php-developers-trip can help?

TTT
+1  A: 

It kind of sounds like you should approach it this way:

1) Code a graph object that will let you query your data the way you want.

2) Write a persistence layer for your object

3) optimize the calls that do your query's in the graph object to use database calls when appropriate (for example, if you need to conserve memory).

Zak
Yes that can be a solution, I was thinking if there were any classes already out there for that but it seems that graph structures have very little attention at the moment and the code you can find is very few. I guess I'm going to have to invent the wheel...
stagas
A: 

Dang, seems like it would be easier to just use a java based graph for now. All these alternatives seem so old school. :)

Warren