-
Notifications
You must be signed in to change notification settings - Fork 5
Examples
Marijn van Wezel edited this page Dec 12, 2022
·
6 revisions
The examples below are written in PHP 7.4. The examples below are copies of the queries you will find in the Neo4j browser after running :play movies
.
To match and return all movies, build your statement like this:
$movie = node("Movie");
$query = query()
->match($movie)
->returning($movie)
->build();
$this->assertStringMatchesFormat("MATCH (%s:Movie) RETURN %s", $query);
To find the actor named "Tom Hanks":
$tom = node()->withProperties([
'name' => 'Tom Hanks',
]);
$query = query()
->match($tom)
->returning($tom)
->build();
$this->assertStringMatchesFormat("MATCH (%s {name: 'Tom Hanks'}) RETURN %s", $query);
To find the movie with the title "Cloud Atlas":
$cloudAtlas = node()->withProperties([
'title' => 'Cloud Atlas',
]);
$query = query()
->match($cloudAtlas)
->returning($cloudAtlas)
->build();
$this->assertStringMatchesFormat("MATCH (%s {title: 'Cloud Atlas'}) RETURN %s", $query);
To find 10 people:
$people = node('Person');
$query = query()
->match($people)
->returning($people->property('name'))
->limit(10)
->build();
$this->assertStringMatchesFormat("MATCH (%s:Person) RETURN %s.name LIMIT 10", $query);
To find movies released in the 1990s:
$nineties = node('Movie');
$query = query()
->match($nineties)
->where([
$nineties->property('released')->gte(1990),
$nineties->property('released')->lt(2000),
])
->returning($nineties->property('title'))
->build();
$this->assertStringMatchesFormat("MATCH (%s:Movie) WHERE ((%s.released >= 1990) AND (%s.released < 2000)) RETURN %s.title", $query);
To list all Tom Hanks movies:
$movies = node();
$tom = node('Person')->withProperties(['name' => 'Tom Hanks']);
$query = query()
->match($tom->relationshipTo($movies, 'ACTED_IN'))
->returning([$tom, $movies])
->build();
$this->assertStringMatchesFormat("MATCH (%s:Person {name: 'Tom Hanks'})-[:ACTED_IN]->(%s) RETURN %s, %s", $query);
To find out who directed "Cloud Atlas":
$directors = node();
$cloudAtlas = node()->withProperties(['title' => 'Cloud Atlas',]);
$query = query()
->match($cloudAtlas->relationshipFrom($directors, 'DIRECTED'))
->returning($directors->property('name'))
->build();
$this->assertStringMatchesFormat("MATCH ({title: 'Cloud Atlas'})<-[:DIRECTED]-(%s) RETURN %s.name", $query);
To find Tom Hanks' co-actors:
$coActors = node();
$tom = node('Person')->withProperties(['name' => 'Tom Hanks']);
$query = query()
->match($tom->relationshipTo(node(), 'ACTED_IN')->relationshipFrom($coActors, 'ACTED_IN'))
->returning($coActors->property('name'))
->build();
$this->assertStringMatchesFormat("MATCH (:Person {name: 'Tom Hanks'})-[:ACTED_IN]->()<-[:ACTED_IN]-(%s) RETURN %s.name", $query);