1. Go to this page and download the library: Download giudicelli/neo4j-php-ogm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
giudicelli / neo4j-php-ogm example snippets
namespace App;
use Neo4j\OGM\NodeManagerInterface;
use App\Entity\Movie;
use App\Entity\Person;
use App\Relationship\ActedIn;
function loadTomHanks(NodeManagerInterface $nm): Person {
$tomHanks = new Person();
$tomHanks
->setName('Tom Hanks')
->setBorn(1956)
->setGender('MALE')
;
$nm->getRepository(Person::class)->save($tomHanks);
$forrestGump = new Movie();
$forrestGump
->setTitle('Forrest Gump')
->setReleased(1994)
->setTagline('The world will never be the same once you\'ve seen it through the eyes of Forrest Gump.')
;
$nm->getRepository(Movie::class)->save($forrestGump);
// This is a relationship
$actedIn = new ActedIn();
$actedIn
->setPerson($tomHanks)
->setMovie($forrestGump)
->setRoles(['Forrest Gump'])
;
$nm->getRepository(ActedIn::class)->save($forrestGump);
// Now we need to refresh both entities
// for the newly linked entities to show up
$nm->getRepository(Person::class)->refresh($tomHanks);
$nm->getRepository(Movie::class)->refresh($forrestGump);
return $tomHanks;
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\EntityInterface;
/**
* @OGM\Entity(label="Movie")
*/
class Movie implements EntityInterface
{
/**
* @OGM\Id()
*/
private $id;
public function getId(): ?int
{
return $this->id;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\RelationshipInterface;
/**
* @OGM\Relationship(type="ACTED_IN")
*/
class ActedIn implements RelationshipInterface
{
/**
* @OGM\Id()
*/
private ?int $id = null;
public function getId(): ?int
{
return $this->id;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\EntityInterface;
/**
* @OGM\Entity(label="Movie")
*/
class Movie implements EntityInterface
{
/**
* @OGM\Id()
*/
private $id;
public function getId(): ?int
{
return $this->id;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\RelationshipInterface;
use App\Entity\Person;
/**
* @OGM\Relationship(type="ACTED_IN")
*/
class ActedIn implements RelationshipInterface
{
/**
* @OGM\Id()
*/
private ?int $id = null;
/**
* @OGM\StartEntity(target=Person::class)
*/
private ?Person $person = null;
public function getId(): ?int
{
return $this->id;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function setPerson(?Person $person): self
{
$this->person = $person;
return $this;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\RelationshipInterface;
use App\Entity\Movie;
use App\Entity\Person;
/**
* @OGM\Relationship(type="ACTED_IN")
*/
class ActedIn implements RelationshipInterface
{
/**
* @OGM\Id()
*/
private ?int $id = null;
/**
* @OGM\StartEntity(target=Person::class)
*/
private ?Person $person = null;
/**
* @OGM\EndEntity(target=Movie::class)
*/
private ?Movie $movie = null;
public function getId(): ?int
{
return $this->id;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function setPerson(?Person $person): self
{
$this->person = $person;
return $this;
}
public function getMovie(): ?Movie
{
return $this->movie;
}
public function setMovie(?Movie $movie): self
{
$this->movie = $movie;
return $this;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\EntityInterface;
/**
* @OGM\Entity(label="Movie")
*/
class Movie implements EntityInterface
{
/**
* @OGM\Id()
*/
private $id;
/**
* @OGM\Property(type="string", nullable=false)
*/
private $title;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\EntityInterface;
/**
* @OGM\Entity(label="Movie")
*/
class Movie implements EntityInterface
{
/**
* @OGM\Id()
*/
private $id;
/**
* @OGM\Property(type="string", nullable=false)
*/
private $title;
/**
* @OGM\Property()
* @OGM\Convert(type="datetime", options={"format":"long_timestamp"})
*/
private $createdAt;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
}
namespace App\Entity;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Model\EntityInterface;
/**
* @OGM\Entity(label="Movie")
*/
class Movie implements EntityInterface
{
/**
* @OGM\Id()
*/
private $id;
/**
* @OGM\Property(type="string", nullable=false)
*/
private $title;
/**
* @OGM\Property()
* @OGM\Convert(type="datetime", options={"format"="long_timestamp"})
*/
private $createdAt;
/**
* @OGM\QueryResult(query="MATCH ({ENTRY})<-[:ACTED_IN]-(actor:Person) RETURN actor.name AS {OUTPUT}",collection=true,limit=2,orderBy={"actor.born"="ASC"})
*/
private $oldestTwoActors;
/**
* @OGM\QueryResult(query="MATCH ({ENTRY})<-[:ACTED_IN]-(actor:Person) RETURN avg({ENTRY}.released - actor.born) AS {OUTPUT}")
*/
private $averageActorsAge;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getOldestTwoActors(): array {
return $this->oldestTwoActors;
}
public function getAverageActorsAge(): float {
return $this->averageActorsAge;
}
}
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Neo4j\OGM\Annotation as OGM;
use Neo4j\OGM\Common\Direction;
use App\Relationship\ActedIn;
use App\Relationship\Directed;
use App\Relationship\Produced;
use App\Relationship\Reviewed;
use Neo4j\OGM\Model\EntityInterface;
/**
* @OGM\Entity(label="Movie")
*/
class Movie implements EntityInterface
{
/**
* @OGM\Id()
*/
private $id;
/**
* @OGM\Property(type="string")
*/
private $title;
/**
* @OGM\Property(type="int")
*/
private $released;
/**
* @OGM\Property(type="string")
*/
private $tagline;
/**
* @OGM\Relation(relationship=ActedIn::class,direction=Direction::INCOMING,collection=true,orderBy={"born"="ASC"},relationshipProperty="actorsMeta")
*/
private $actors;
private $actorsMeta;
/**
* @OGM\Relation(relationship=ActedIn::class,direction=Direction::INCOMING,collection=true,orderBy={"born"="ASC"},relationshipProperty="femaleActorsMeta",filters={"gender"="FEMALE"})
*/
private $femaleActors;
private $femaleActorsMeta;
/**
* @OGM\Relation(relationship=ActedIn::class,direction=Direction::INCOMING,collection=true,orderBy={"born"="ASC"},relationshipProperty="femaleActorsMeta",filters={"gender"="MALE"})
*/
private $maleActors;
private $maleActorsMeta;
/**
* @OGM\Relation(relationship=Directed::class,direction=Direction::INCOMING,collection=true)
*/
private $directors;
/**
* @OGM\Relation(relationship=Produced::class,direction=Direction::INCOMING,collection=true)
*/
private $producers;
/**
* @OGM\Relation(relationship=Reviewed::class,direction=Direction::INCOMING,collection=true,relationshipProperty="reviewsMeta")
*/
private $reviews;
private $reviewsMeta;
/**
* @OGM\QueryResult(query="MATCH ({ENTRY})<-[r:REVIEWED]-(:Person) RETURN avg(r.rating) AS {OUTPUT}")
*/
private $rating;
/**
* @OGM\Relation(relationship=ActedIn::class,direction=Direction::INCOMING,collection=true,limit=2,orderBy={"born"="ASC"})
*/
private $oldestTwoActors;
/**
* @OGM\QueryResult(query="MATCH ({ENTRY})<-[:ACTED_IN]-(actor:Person) RETURN avg({ENTRY}.released - actor.born) AS {OUTPUT}")
*/
private $averageActorsAge;
public function __construct()
{
$this->actors = new ArrayCollection();
$this->actorsMeta = new ArrayCollection();
$this->directors = new ArrayCollection();
$this->producers = new ArrayCollection();
$this->reviews = new ArrayCollection();
$this->reviewsMeta = new ArrayCollection();
$this->oldestTwoActors = new ArrayCollection();
}
public function getId(): int
{
return $this->id;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getReleased(): int
{
return $this->released;
}
public function setReleased(int $released): self
{
$this->released = $released;
return $this;
}
public function getTagline(): string
{
return $this->tagline;
}
public function setTagline(string $tagline): self
{
$this->tagline = $tagline;
return $this;
}
public function getActors(): Collection
{
return $this->actors;
}
public function getActorsMeta(): Collection
{
return $this->actorsMeta;
}
public function getFemaleActors(): Collection
{
return $this->femaleActors;
}
public function getFemaleActorsMeta(): Collection
{
return $this->femaleActorsMeta;
}
public function getMaleActors(): Collection
{
return $this->maleActors;
}
public function getMaleActorsMeta(): Collection
{
return $this->maleActorsMeta;
}
public function getDirectors(): Collection
{
return $this->directors;
}
public function getProducers(): Collection
{
return $this->producers;
}
public function getReviews(): Collection
{
return $this->reviews;
}
public function getReviewsMeta(): Collection
{
return $this->reviewsMeta;
}
public function getRating(): ?float
{
return $this->rating;
}
public function getOldestTwoActors(): Collection
{
return $this->oldestTwoActors;
}
public function getAverageActorsAge(): float
{
return $this->averageActorsAge;
}
}
$node = $repository->findOneBy(['id()' => $id]);
namespace App;
use Neo4j\OGM\NodeManagerInterface;
use App\Entity\Movie;
use App\Entity\Person;
use App\Relationship\ActedIn;
/** @return Person[]|null */
function loadCostars(NodeManagerInterface $nm, string $name): ?array {
$query = 'MATCH (p:Person)-[:ACTED_IN]->(:Movie)<-[:ACTED_IN]-(costar:Person)';
$query .= PHP_EOL.'WHERE p.name = $name';
$query .= PHP_EOL.'WITH DISTINCT costar AS costar';
$params = ['name' => $name];
return $nm->getRepository(Person::class)->findByQuery(
'costar',
$query,
$params
);
}
cli
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.