persist() public method

Persist an entity
public persist ( object $entity, integer $ttl = null )
$entity object Entity object to persist
$ttl integer Optional TTL if the driver supports it, seconds past current time
Example #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();
     }
 }
Example #2
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testIdQuery(EntityManager $em)
 {
     $a = new IndexedEntity();
     $a->setId1('1');
     $a->setId2('first');
     $a->setAlpha('alpha1');
     $a->setBravo(1);
     $a->setCharlie(true);
     $b = new IndexedEntity();
     $b->setId1('2');
     $b->setId2('second');
     $b->setAlpha('alpha2');
     $b->setBravo(2);
     $b->setCharlie(false);
     $em->persist($a)->persist($b)->flush();
     $result = $em->indexedQuery(new IndexedQuery(IndexedEntity::class, ['@id' => '1.fir*']));
     $this->assertCount(1, $result);
     /** @var IndexedEntity $entity */
     $entity = $result->current();
     $this->assertEquals('1', $entity->getId1());
     $this->assertEquals('first', $entity->getId2());
     $this->assertEquals('alpha1', $entity->getAlpha());
     $result = $em->indexedQuery(new IndexedQuery(IndexedEntity::class, ['@id' => '*']));
     $this->assertGreaterThanOrEqual(2, count($result));
     $result = $em->indexedQuery(new IndexedQuery(IndexedEntity::class, ['@id' => '*', 'ab' => 'alpha1*']));
     $this->assertCount(1, $result);
     /** @var IndexedEntity $entity */
     $entity = $result->current();
     $this->assertEquals('1', $entity->getId1());
     $this->assertEquals('first', $entity->getId2());
     $this->assertEquals('alpha1', $entity->getAlpha());
 }
Example #3
0
 /**
  * Testing race conditions of new entities, without a flush between persist calls
  *
  * @see: docs/RaceConditions.md
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testOneToManyRaceNoFlush(EntityManager $em)
 {
     $time = new \DateTime();
     $article1 = new Article();
     $article1->setId(201)->setTitle('Article 201')->setTimeCreated($time)->setLastModified($time);
     $category1 = new Category();
     $category1->setId(201)->setName('Category 201');
     $category1->addArticle($article1);
     $em->persist($category1)->persist($article1)->flush();
     /** @var Article|OrmProxyInterface $r_article */
     $r_article = $em->retrieve(Article::class, 201);
     $this->assertEquals('Article 201', $r_article->getTitle());
     // Should make DB query here
     $r_category = $r_article->getCanonicalCategory();
     $this->assertTrue($r_category instanceof Category);
     // Check inverse side too -
     /** @var Category|OrmProxyInterface $ir_category */
     $ir_category = $em->retrieve(Category::class, 201);
     $this->assertEquals('Category 201', $ir_category->getName());
     // Should make DB query here
     $ir_articles = $ir_category->getArticles();
     $this->assertCount(1, $ir_articles);
     $ir_article = $ir_articles[0];
     $this->assertTrue($ir_article instanceof Article);
 }
Example #4
0
 /**
  * Writes the session data to the storage.
  *
  * @param string $sessionId Session ID , see http://php.net/function.session-id
  * @param string $data      Serialized session data to save
  * @return bool true on success, false on failure
  */
 public function write($sessionId, $data)
 {
     /** @var SessionInterface $session */
     $session = new $this->session_class();
     $session->setId($sessionId);
     $session->setData($data);
     $this->entity_manager->persist($session, $this->ttl)->flush();
     return true;
 }
Example #5
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();
     }
 }
Example #6
0
 /**
  * @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) {
     }
 }
Example #8
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);
 }
Example #9
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());
 }
Example #10
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testRelatedIndexDeletion(EntityManager $em)
 {
     $home = new Address();
     $home->setId(44)->setStreet('Oxford St');
     $work = new Address();
     $work->setId(45)->setStreet('George St');
     $user = new User();
     $user->setId(23)->setName('Barry')->setAddress($home);
     $em->persist($user)->persist($home)->persist($work)->flush();
     /** @var User|OrmProxyInterface $user_home */
     $user_home = $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $home->getId());
     $this->assertEquals(23, $user_home->getId());
     $this->assertEquals('Oxford St', $user_home->getAddress()->getStreet());
     $slug = $user_home->getIndexOriginalValue('slug');
     $this->assertEquals('23.44', $slug);
     $user_home->setName('Other Barry');
     $user_home->setAddress($work);
     $em->persist($user_home)->flush();
     /** @var User $user_work */
     $user_work = $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $work->getId());
     $this->assertEquals(23, $user_work->getId());
     $this->assertEquals('George St', $user_work->getAddress()->getStreet());
     $this->setExpectedException(NotFoundException::class);
     $em->retrieveByIndex(User::class, 'slug', $user->getId() . '.' . $home->getId());
 }
Example #11
0
 /**
  * @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());
 }
Example #12
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testNullObjectSerialisation(EntityManager $em)
 {
     $product = new Product();
     $product->setId(701)->setEnum(null);
     $em->persist($product)->flush();
     /** @var Product $r_product */
     $r_product = $em->retrieve(Product::class, '701');
     $this->assertNull($r_product->getEnum());
 }
Example #13
0
 /**
  * Same as above test, except we'll delete the category
  *
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testDeleteIndicesAndRelationshipsAlt(EntityManager $em)
 {
     $article1 = new RefArticle();
     $article1->setId(502)->setTitle('Ref Article 502');
     $category1 = new RefCategory();
     $category1->setId(533)->setName('Ref Category 533');
     $article1->setCanonicalCategory($category1);
     $em->persist($category1)->persist($article1)->flush();
     $this->assertTrue($this->exists($em, 'article', '502'));
     $mto = $this->getRelKey($em, 'article', 'category', '502', 'canonical_category', RelationshipType::MANYTOONE());
     $this->assertEquals('533', $em->getDriver()->getSingleValueIndex($mto));
     // Not inversed:
     $otm = $this->getRelKey($em, 'category', 'article', '533', 'articles', RelationshipType::ONETOMANY());
     $this->assertNotContains('502', $em->getDriver()->getMultiValueIndex($otm));
     // Ref exists:
     $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '533'));
     $ref = (string) new Ref(RefArticle::class, '502', 'canonical_category');
     $this->assertCount(1, $refs);
     $this->assertEquals($ref, (string) $refs[0]);
     /** @var RefCategory $category */
     $category = $em->retrieve(RefCategory::class, 533);
     $em->delete($category)->flush();
     $this->assertFalse($this->exists($em, 'category', '533'));
     $this->assertNull($em->getDriver()->getSingleValueIndex($mto));
     $this->assertNotContains('502', $em->getDriver()->getMultiValueIndex($otm));
     // Ref no longer needed:
     $refs = $em->getDriver()->getRefs($this->getEntityRefKey($em, 'category', '533'));
     $this->assertNotContains($ref, $refs);
 }
Example #14
0
 /**
  * @dataProvider entityManagerDataProvider
  * @param EntityManager $em
  */
 public function testOneToOneBreaking(EntityManager $em)
 {
     $ed = new User();
     $ed->setId(900)->setName('Ed');
     $jane = new User();
     $jane->setId(901)->setName('Jane');
     $address = new Address();
     $address->setId(951)->setStreet('Howzer-Black St');
     $ed->setAddress($address);
     $em->persist($address)->persist($ed)->persist($jane)->flush();
     $em->refresh($ed);
     $em->refresh($jane);
     $em->refresh($address);
     $jane->setAddress($address);
     $em->persist($jane)->flush();
     $em->refresh($ed);
     $em->refresh($jane);
     $em->refresh($address);
     $this->assertNull($ed->getAddress());
     $this->assertEquals('Howzer-Black St', $jane->getAddress()->getStreet());
     $this->assertEquals('Jane', $address->getUser()->getName());
 }