public function setInversedAssociation($baseInstance, $otherInstance, $relationshipKey) { $class = get_class($otherInstance); if (false !== get_parent_class($otherInstance)) { $class = get_parent_class($otherInstance); } $assoc = $this->classMetadata->getRelationship($relationshipKey); if ($assoc->hasMappedByProperty()) { $mappedBy = $assoc->getMappedByProperty(); $reflClass = $this->getReflectionClass($class); $property = $reflClass->getProperty($mappedBy); $property->setAccessible(true); $otherClassMetadata = $this->entityManager->getClassMetadataFor(get_class($otherInstance)); if ($otherClassMetadata instanceof RelationshipMetadata || $otherClassMetadata instanceof RelationshipEntityMetadata) { return; } if ($otherClassMetadata->getRelationship($mappedBy)->isCollection()) { if (null === $property->getValue($otherInstance)) { $mt = $otherClassMetadata->getRelationship($mappedBy); $lazy = new LazyRelationshipCollection($this->entityManager, $otherInstance, $mt->getTargetEntity(), $mt, $baseInstance); $property->setValue($otherInstance, $lazy); } else { $property->getValue($otherInstance)->addInit($baseInstance); } } else { $property->setValue($otherInstance, $baseInstance); } } }
/** * @param $class * * @return \GraphAware\Neo4j\OGM\Persister\RelationshipEntityPersister */ public function getRelationshipEntityPersister($class) { if (!array_key_exists($class, $this->relationshipEntityPersisters)) { $classMetadata = $this->entityManager->getRelationshipEntityMetadata($class); $this->relationshipEntityPersisters[$class] = new RelationshipEntityPersister($this->entityManager, $class, $classMetadata); } return $this->relationshipEntityPersisters[$class]; }
<?php require_once __DIR__ . '/../../../vendor/autoload.php'; use GraphAware\Neo4j\OGM\EntityManager; use Movies\Movie; use Movies\Person; use Movies\User; // Entity manager setup $em = EntityManager::create('http://localhost:7676'); playMovies($em->getDatabaseDriver()); // Retrieving a node $personRepository = $em->getRepository(Person::class); $tomHanks = $personRepository->findOneBy('name', 'Tom Hanks'); // Updating an entity $actor = new Person('Kevin Ross', 1976); $em->persist($actor); $em->flush(); // Managing after load $tomHanks->setBorn(1990); $em->flush(); // Clearing $em->clear(); // Relationships retrieval $tomHanks = $em->getRepository(Person::class)->findOneBy('name', 'Tom Hanks'); echo sprintf('Tom Hanks played in %d movies', count($tomHanks->getMovies())) . PHP_EOL; /** @var Movie $movie */ foreach ($tomHanks->getMovies() as $movie) { echo $movie->getTitle() . PHP_EOL; echo count($movie->getActors()) . PHP_EOL; } $em->clear();
private function createEntityManager() { $this->em = EntityManager::create(sprintf('%s://%s:%s@%s:%s', getenv('NEO4J_SCHEMA'), getenv('NEO4J_USER'), getenv('NEO4J_PASSWORD'), getenv('NEO4J_HOST'), getenv('NEO4J_PORT')), __DIR__ . '/../../_var/cache'); }