flush() public method

Execute the current unit of work
public flush ( )
Esempio n. 1
0
 /**
  * Re-persist an array of records
  *
  * @param object[] $records
  * @param Entity   $metadata
  * @param int      $batch_size
  */
 private function rebuildRecords($records, Entity $metadata, $batch_size)
 {
     $count = 0;
     /** @var OrmProxyInterface $record */
     foreach ($records as $record) {
         $this->entity_manager->persist($record);
         if (++$count == $batch_size) {
             $this->entity_manager->flush();
             $count = 0;
         }
     }
     if ($count) {
         $this->entity_manager->flush();
     }
 }
Esempio n. 2
0
 /**
  * Update the users password and/or salt
  *
  * @param User   $user
  * @param string $password New password
  * @param string $salt     If Ommitted, the salt will not be updated
  * @param bool   $flush    Flush the entity manager on completion
  */
 public function updateUserCredentials(User $user, $password, $salt = null, $flush = true)
 {
     if (!$salt) {
         $salt = $user->getSalt() ?: $this->generateSalt();
     }
     $encoder = $this->encoder_factory->getEncoder($user);
     $password = $encoder->encodePassword($password, $salt);
     $user->setPassword($password);
     $user->setSalt($salt);
     $this->entity_manager->persist($user);
     if ($flush) {
         $this->entity_manager->flush();
     }
 }
Esempio n. 3
0
 /**
  * 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;
 }
Esempio n. 4
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRebuildSchema(EntityManager $em)
 {
     $create_time = new \DateTime("2015-01-01 12:15:03+0000");
     $product = new Product();
     $product->setId(333)->setName('Test Product')->setDescription("lorem ipsum")->setActive(true)->setCreateTime($create_time)->setPrice(12.45);
     $em->persist($product);
     $em->flush();
     // Rebuild and remove a property
     $maintenance = new Maintenance($em);
     $maintenance->rebuild(ProductLess::class);
     $p1 = $em->retrieve(ProductLess::class, '333', false);
     $this->assertEquals('Test Product', $p1->getName());
     $p2 = $em->retrieve(Product::class, '333', false);
     $this->assertEquals('Test Product', $p2->getName());
     $this->assertEmpty($p2->getDescription());
     // Rebuild and add a property
     $maintenance = new Maintenance($em);
     $maintenance->rebuild(ProductMore::class);
     $p3 = $em->retrieve(ProductMore::class, '333', false);
     $this->assertEquals('Test Product', $p3->getName());
     $this->assertEmpty($p3->getShortDescription());
     $p4 = $em->retrieve(Product::class, '333', false);
     $this->assertEquals('Test Product', $p4->getName());
 }
Esempio n. 5
0
 /**
  * @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());
 }
Esempio n. 6
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testIntercepts(EntityManager $em)
 {
     $em->getDispatcher()->addListener(Event::PRE_RETRIEVE, function (RetrieveEvent $event) {
         // Return something completely different instead
         $event->setReturnValue(new Article());
         $event->setAbort(true);
     });
     $em->getDispatcher()->addListener(Event::POST_PERSIST, function (PersistEvent $event) {
         /** @var Product $entity */
         $entity = $event->getEntity();
         $this->assertTrue($entity instanceof Product);
         $entity->setName('Persisted Product');
     });
     $product = new Product();
     $product->setId(111)->setName('New Product')->setCreateTime(new \DateTime());
     $em->persist($product);
     $em->flush();
     $this->assertEquals('Persisted Product', $product->getName());
     $retrieved = $em->retrieve(Product::class, 111);
     $this->assertTrue($retrieved instanceof Article);
 }
Esempio n. 7
0
 /**
  * @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);
 }