/** * 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); }
/** * @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()); }
/** * 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); } }
/** * 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(); }
/** * 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; }