Ejemplo n.º 1
0
 /**
  * Get all foreign entities ordered by a sort column
  *
  * If you have applied a limit to the query but need to know the full size of the unfiltered set, you must set
  * $check_full_set_size to true to gather this information at the expense of a second database query.
  *
  * @param SortedQuery $query
  * @param bool        $check_full_set_size
  * @param bool        $use_cache
  * @return QueryResult
  */
 public function sortedQuery(SortedQuery $query, $check_full_set_size = false, $use_cache = true)
 {
     $metadata = $this->getMapper()->getEntityMetadata($query->getClassName());
     if ($query->getRelationshipName()) {
         // Entity relationship based query
         $reader = new Reader($metadata, $query->getEntity());
         $relationship = $metadata->getRelationshipByName($query->getRelationshipName());
         if (!$relationship) {
             throw new InvalidArgumentException('Relationship "' . $query->getRelationshipName() . '" does not exist');
         }
         // Important, else the QueryResult class will try to hydrate the wrong entity
         $query->setClassName($relationship->getTarget());
         $key = $this->getKeyScheme()->getSortIndexKey($relationship, $query->getSortBy(), $reader->getId());
     } else {
         // Table based query
         $key = $this->getKeyScheme()->getTableSortKey($metadata->getTableName(), $query->getSortBy());
     }
     $results = $this->getDriver()->getSortedIndex($key, $query->getDirection() == Direction::DESC(), $query->getStart(), $query->getEnd());
     if (!$query->getStart() && !$query->getEnd()) {
         $full_size = count($results);
     } elseif ($check_full_set_size) {
         $full_size = $this->getDriver()->getSortedIndexSize($key);
     } else {
         $full_size = null;
     }
     return new QueryResult($this->entity_manager, $query, $results, $full_size, $use_cache);
 }
Ejemplo n.º 2
0
 /**
  * Serialise the entity
  *
  * @param Entity $metadata
  * @param object $entity
  * @return SerialisedData
  */
 public function serialise(Entity $metadata, $entity)
 {
     $data = new \stdClass();
     $reader = new Reader($metadata, $entity);
     foreach ($metadata->getColumns() as $column) {
         $this->assignValue($data, $column, $reader->getPropertyValue($column->getProperty()));
     }
     return new SerialisedData(self::SERIALISER_CODE, json_encode($data, self::ENCODE_OPTIONS, self::ENCODE_DEPTH));
 }
Ejemplo n.º 3
0
 /**
  * Get the metadata for an entity, including column information
  *
  * @param string|object $entity Entity or class name of the entity
  * @return Entity
  */
 public function getEntityMetadata($entity)
 {
     $class_name = Reader::getEntityClassName($entity);
     if (!isset($this->metadata_cache[$class_name])) {
         $parser = new AnnotationMetadataParser($class_name);
         $this->metadata_cache[$class_name] = $parser->getEntityMetadata();
     }
     return $this->metadata_cache[$class_name];
 }
Ejemplo n.º 4
0
 public function testStuff()
 {
     $mapper = new AnnotationMapper();
     $product = new Product();
     $product_meta = $mapper->getEntityMetadata(Reader::getEntityClassName($product));
     $this->assertEquals("products", $product_meta->getTableName());
     $bad = new BadEntity();
     $bad_meta = $mapper->getEntityMetadata(Reader::getEntityClassName($bad));
     $this->assertEquals("bad_entity", $bad_meta->getTableName());
 }
Ejemplo n.º 5
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testIndex(EntityManager $em)
 {
     $entity = new IndexedEntity();
     $entity->setId1(100)->setId2('id2');
     $entity->setAlpha('alpha')->setBravo('200')->setCharlie(true);
     $metadata = $em->getMapper()->getEntityMetadata($entity);
     $reader = new Reader($metadata, $entity);
     $this->assertEquals('100.id2', $reader->getId());
     $indices = $metadata->getIndices();
     $this->assertCount(3, $indices);
     $ab = $metadata->getIndexByName('ab');
     $this->assertContains('alpha', $ab->getColumns());
     $this->assertContains('bravo', $ab->getColumns());
     $this->assertCount(2, $ab->getColumns());
     $this->assertEquals('alpha.200', $reader->getIndexValue($ab));
     $bc = $metadata->getIndexByName('bc');
     $this->assertContains('bravo', $bc->getColumns());
     $this->assertContains('getCharlie', $bc->getMethods());
     $this->assertCount(1, $bc->getColumns());
     $this->assertCount(1, $bc->getMethods());
     $this->assertEquals('200.1', $reader->getIndexValue($bc));
     $b = $metadata->getIndexByName('b');
     $this->assertContains('bravo', $b->getColumns());
     $this->assertCount(1, $b->getColumns());
     $this->assertEquals('200', $reader->getIndexValue($b));
     $em->persist($entity)->flush();
     /** @var IndexedEntity $retrieved */
     $retrieved = $em->retrieve(self::INDEXED_ENTITY, '100.id2');
     $retrieved->setAlpha('omega')->setId1(101);
     $em->persist($retrieved)->flush();
     try {
         $em->retrieveByIndex(self::INDEXED_ENTITY, 'ab', 'alpha.200');
         $this->fail("Former index was found");
     } catch (NotFoundException $e) {
     }
     /** @var IndexedEntity $retrieved_by_index */
     $retrieved_by_index = $em->retrieveByIndex(self::INDEXED_ENTITY, 'ab', 'omega.200');
     $this->assertEquals(101, $retrieved_by_index->getId1());
     $this->assertEquals('id2', $retrieved_by_index->getId2());
     $this->assertSame('omega', $retrieved_by_index->getAlpha());
     $this->assertSame(200, $retrieved_by_index->getBravo());
     $this->assertSame(true, $retrieved_by_index->getCharlie());
 }
Ejemplo n.º 6
0
 /**
  * Traverse an array of indices and persist them
  *
  * @param Index[] $indices
  * @param object  $entity
  * @param Reader  $reader
  * @param string  $local_id
  */
 private function traverseDeleteIndices(array $indices, $entity, Reader $reader, $local_id)
 {
     $is_proxy = $entity instanceof OrmProxyInterface;
     foreach ($indices as $index) {
         if ($is_proxy) {
             /** @var OrmProxyInterface $entity */
             $index_value = $entity->getIndexOriginalValue($index->getName());
         } else {
             $index_value = $reader->getIndexValue($index);
         }
         $this->getDriver()->clearSingleValueIndex($this->getKeyScheme()->getIndexKey($index, $index_value));
     }
 }
Ejemplo n.º 7
0
 /**
  * Get the metadata for an entity, including column information
  *
  * @param string|object $entity Entity or class name of the entity
  * @return Entity
  */
 public function getEntityMetadata($entity)
 {
     foreach ($this->mappers as $mapper) {
         try {
             return $mapper->getEntityMetadata($entity);
         } catch (NoMetadataException $e) {
         }
     }
     if (is_object($entity)) {
         $class = Reader::getEntityClassName($entity);
     } else {
         $class = $entity;
     }
     throw new NoMetadataException("No metadata found for '" . $class . "'");
 }
Ejemplo n.º 8
0
 /**
  * Set a forward multi-value relationship index
  *
  * When setting forward indices the set is always cleared and re-added. This will fully synchronise the list and
  * there is no need to calculate a delta, however it may be slow on large sets.
  *
  * Inverse relationships should always be updated via a delta operation.
  *
  * @param string        $key
  * @param object[]|null $foreign_entities
  */
 private function setMultiValueRelationship($key, $foreign_entities)
 {
     $this->getDriver()->clearMultiValueIndex($key);
     if ($foreign_entities) {
         $values = [];
         foreach ($foreign_entities as $entity) {
             $rel_metadata = $this->getMapper()->getEntityMetadata($entity);
             $rel_reader = new Reader($rel_metadata, $entity);
             $values[] = $rel_reader->getId();
         }
         $this->getDriver()->addMultiValueIndex($key, $values);
     }
 }
Ejemplo n.º 9
0
 /**
  * Get the full ID of an entity
  *
  * @param object $entity
  * @return string
  */
 protected function getEntityId($entity)
 {
     $metadata = $this->getMapper()->getEntityMetadata(Reader::getEntityClassName($entity));
     $reader = new Reader($metadata, $entity);
     return $reader->getId();
 }
Ejemplo n.º 10
0
 /**
  * Set entity class name by name or entity object
  *
  * @param string|object $class_name
  * @return void
  */
 public function setClassName($class_name)
 {
     $this->class_name = Reader::getEntityClassName($class_name);
 }
Ejemplo n.º 11
0
 /**
  * Will force a database update of an entity
  *
  * This will also convert a fresh entity to an OrmProxyInterface.
  *
  * @param object $entity
  * @return object
  */
 public function refresh(&$entity)
 {
     $metadata = $this->getMapper()->getEntityMetadata($entity);
     $reader = new Reader($metadata, $entity);
     $entity = $this->retrieve($metadata->getClassName(), $reader->getId(), false);
     return $entity;
 }
Ejemplo n.º 12
0
 /**
  * Get the metadata for an entity, including column information
  *
  * If you do not provide a $relative_mapper then relationship metadata will not be hydrated.
  *
  * @param string|object $entity Entity or class name of the entity
  * @return Entity
  */
 public function getEntityMetadata($entity)
 {
     if (is_object($entity)) {
         $class = Reader::getEntityClassName($entity);
     } else {
         $class = $entity;
     }
     $this->processMaps();
     if (array_key_exists($class, $this->entities)) {
         return $this->entities[$class];
     } else {
         throw new NoMetadataException("No metadata is registered for class '" . $class . "'");
     }
 }