Get the underlying driver
public getDriver ( ) : Bravo3\Orm\Drivers\DriverInterface | ||
Результат | Bravo3\Orm\Drivers\DriverInterface |
/** * @dataProvider entityManagerDataProvider * @param EntityManager $em */ public function testRefs(EntityManager $em) { $members = $em->getDriver()->getMultiValueIndex($em->getKeyScheme()->getEntityRefKey('leaf', 'leaf1')); $this->assertCount(0, $members); $leaf = (new Leaf())->setId('leaf1'); $owner = (new Owner())->setId('owner1')->setLeaf([$leaf]); $em->persist($leaf)->persist($owner)->flush(); $members = $em->getDriver()->getMultiValueIndex($em->getKeyScheme()->getEntityRefKey('leaf', 'leaf1')); $this->assertCount(1, $members); $ref = new Ref(Owner::class, 'owner1', 'leaf'); $this->assertEquals((string) $ref, $members[0]); $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id')); $this->assertCount(1, $leaves); $em->refresh($owner); $em->refresh($leaf); $leaf->setPublished(false); $em->persist($leaf)->flush(); $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id')); $this->assertCount(0, $leaves); $leaf->setPublished(true); $em->persist($leaf)->flush(); $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id')); $this->assertCount(1, $leaves); $em->delete($leaf)->flush(); $leaves = $em->sortedQuery(new SortedQuery($owner, 'leaf', 'id')); $this->assertCount(0, $leaves); }
/** * @dataProvider entityManagerDataProvider * @param EntityManager $em * @expectedException \Bravo3\Orm\Exceptions\CorruptedEntityException */ public function testSortedQueryThrowsNotFoundException(EntityManager $em) { $category = (new Category())->setId(5000); $article1 = (new Article())->setId(5001)->setTitle('A'); $article2 = (new Article())->setId(5002)->setTitle('B'); $article3 = (new Article())->setId(5003)->setTitle('C'); $category->addArticle($article1); $category->addArticle($article2); $category->addArticle($article3); $em->persist($article1)->persist($article2)->persist($article3)->persist($category)->flush(); // Forcefully break the relationship via the driver manually $em->getDriver()->delete($em->getKeyScheme()->getEntityKey('article', '5001')); $em->getDriver()->flush(); $category = $em->retrieve(Category::class, 5000, false); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date')); // Iterating through these results should trigger an exception foreach ($results as $result) { } }
/** * Hydrate a relationship * * @param Relationship $relative * @return $this */ public function hydrateRelative(Relationship $relative) { $this->entity_manager->getDriver()->debugLog("Hydrating relative for " . $this->metadata->getTableName() . "[" . $this->getReader()->getId() . "]::" . $relative->getName()); $setter = $relative->getSetter(); $key = $this->entity_manager->getKeyScheme()->getRelationshipKey($relative, $this->entity_manager->getMapper()->getEntityMetadata($relative->getSource())->getTableName(), $this->entity_manager->getMapper()->getEntityMetadata($relative->getTarget())->getTableName(), $this->getReader()->getId()); if (RelationshipType::isMultiIndex($relative->getRelationshipType())) { $items = []; $ids = $this->entity_manager->getDriver()->getMultiValueIndex($key); foreach ($ids as $id) { $items[] = $this->entity_manager->retrieveEntityOrNew($relative->getTarget(), $id); } $this->proxy->{$setter}($items); } else { $id = $this->entity_manager->getDriver()->getSingleValueIndex($key); if ($id) { $this->proxy->{$setter}($this->entity_manager->retrieve($relative->getTarget(), $id)); } } return $this; }
/** * Get the driver belonging to the entity manager * * @return DriverInterface */ protected function getDriver() { return $this->entity_manager->getDriver(); }
/** * Does a native driver lookup to see if an entity key exists * * @param EntityManager $em * @param string $table * @param string $id * @return bool */ protected function exists(EntityManager $em, $table, $id) { try { $em->getDriver()->retrieve($this->getEntityKey($em, $table, $id)); return true; } catch (NotFoundException $e) { return false; } }
/** * @dataProvider entityManagerDataProvider * @param EntityManager $em */ public function testRebuildIndex(EntityManager $em) { // Create an article with a slug $article = new SluggedArticle(); $article->setId(8343)->setName('foo')->setSlug('bar'); $em->persist($article)->flush(); // Confirm slug works /** @var SluggedArticle $article */ $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false); $this->assertEquals('foo', $article->getName()); $index = $em->getMapper()->getEntityMetadata($article)->getIndexByName('slug'); // Corrupt the slug, two steps required: // 1. Set a new slug $em->getDriver()->setSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'evil'), $article->getId()); // 2. Remove the correct slug $em->getDriver()->clearSingleValueIndex($em->getKeyScheme()->getIndexKey($index, 'bar')); $em->getDriver()->flush(); // Confirm old slug no longer works try { $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false); $this->fail('Old index succeeded'); } catch (NotFoundException $e) { } // Confirm new slug does work $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false); $this->assertEquals('foo', $article->getName()); // Run maintenance over the table, this should correct the slug $maintenance = new Maintenance($em); $maintenance->rebuild(SluggedArticle::class); // Confirm correct slug works $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'bar', false); $this->assertEquals('foo', $article->getName()); // The corrupted slug should still work, this is unideal, but there is no reference to it for the maintenance // service to know to remove it $article = $em->retrieveByIndex(SluggedArticle::class, 'slug', 'evil', false); $this->assertEquals('foo', $article->getName()); }