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.
public sortedQuery ( |
||
$query | ||
$check_full_set_size | boolean | |
$use_cache | boolean | |
리턴 |
/** * @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 */ public function testTableSorting(EntityManager $em) { $user1 = new SortedUser(); $user1->setId(1)->setName('User 1')->setActive(true); $user2 = new SortedUser(); $user2->setId(2)->setName('User 2')->setActive(false); $user3 = new SortedUser(); $user3->setId(3)->setName('User 3')->setActive(true); $em->persist($user1)->persist($user2)->persist($user3)->flush(); $query = $em->sortedQuery(new SortedTableQuery(SortedUser::class, 'name_all')); $this->assertEquals(3, $query->count()); $this->assertEquals('User 1', $query[0]->getName()); $this->assertEquals('User 2', $query[1]->getName()); $this->assertEquals('User 3', $query[2]->getName()); $query = $em->sortedQuery(new SortedTableQuery(SortedUser::class, 'name_active')); $this->assertEquals(2, $query->count()); $this->assertEquals('User 1', $query[0]->getName()); $this->assertEquals('User 3', $query[1]->getName()); }
/** * @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) { } }
/** * Deletes all items in the pool. * * @return PoolInterface */ public function clear() { $items = $this->em->sortedQuery(new SortedTableQuery($this->entity_class, 'key'), false, false); $index = 0; $items->rewind(); while ($items->valid()) { try { $item = $items->current(); $this->em->delete($item); if (++$index % 100 == 0) { $this->em->flush(); } } catch (NotFoundException $e) { // Assume item expired, skip } $items->next(); } $this->em->flush(); return $this; }
/** * @dataProvider entityManagerDataProvider * @param EntityManager $em */ public function testSortOrder(EntityManager $em) { $category = new Category(); $category->setId(600); $em->persist($category); for ($i = 0; $i < 15; $i++) { $article = new Article(); $article->setId(601 + $i); $article->setTitle('Art ' . (601 + $i)); $time = new \DateTime(); $time->modify('+' . ($i + 1) . ' minutes'); $article->setSortDate($time); $article->setCanonicalCategory($category); $em->persist($article); } $em->flush(); /** @var Article $article */ // Date sorting - $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date')); $this->assertCount(15, $results); $article = $results[0]; $this->assertEquals('Art 601', $article->getTitle()); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC())); $this->assertCount(15, $results); $this->assertEquals(15, $results->getFullSize()); $article = $results[0]; $this->assertEquals('Art 615', $article->getTitle()); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC(), 5, -6), true); $this->assertCount(5, $results); $this->assertEquals(15, $results->getFullSize()); $article = $results[0]; $this->assertEquals('Art 610', $article->getTitle()); $article = $results[4]; $this->assertEquals('Art 606', $article->getTitle()); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 2, 5)); $this->assertCount(4, $results); $this->assertNull($results->getFullSize()); $article = $results[0]; $this->assertEquals('Art 603', $article->getTitle()); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::ASC(), 20, 29)); $this->assertCount(0, $results); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title')); $this->assertCount(15, $results); $article = $results[0]; $this->assertEquals('Art 601', $article->getTitle()); // Lexicographic sorting - $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title')); $this->assertCount(15, $results); $article = $results[0]; $this->assertEquals('Art 601', $article->getTitle()); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'title', Direction::DESC())); $this->assertCount(15, $results); $article = $results[0]; $this->assertEquals('Art 615', $article->getTitle()); // Modify an entity's sort-by column $article = $em->retrieve(Article::class, 609); $time = $article->getSortDate(); $time->modify('+1 day'); $article->setSortDate($time); $em->persist($article)->flush(); $results = $em->sortedQuery(new SortedQuery($category, 'articles', 'sort_date', Direction::DESC())); $this->assertCount(15, $results); $article = $results[0]; $this->assertEquals('Art 609', $article->getTitle()); }
/** * @dataProvider entityManagerDataProvider * @param EntityManager $em */ public function testReverseConditionalRelationship(EntityManager $em) { $category = new Category(); $category->setId(2000)->setName('Conditional Category'); $em->persist($category)->flush(); for ($i = 65; $i < 76; $i++) { $article = new Article(); $article->setId($i)->setTitle('Conditional Article #' . $i); if ($i == 73) { $article->setPublished(false); } else { $article->setPublished(true); } $article->setCategory($category); $em->persist($article); } for ($i = 65; $i < 76; $i++) { $asset = new Asset(); $asset->setId($i)->setTitle('Conditional Asset #' . $i); if ($i == 73) { $asset->setPublished(false); } else { $asset->setPublished(true); } $asset->setCategory($category); $em->persist($asset); } $em->flush(); $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'last_modified')); $this->assertCount(10, $articles); $articles = $em->sortedQuery(new SortedQuery($category, 'articles', 'id')); $this->assertCount(11, $articles); $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'last_modified')); $this->assertCount(10, $assets); $assets = $em->sortedQuery(new SortedQuery($category, 'assets', 'id')); $this->assertCount(11, $assets); }