/** * @param \DataObject $object * @param Field $field */ public function write(\DataObject $object, Field $field) { $className = $object->class; // cache has_extension call if (!isset($this->isVersioned[$className])) { $this->isVersioned[$className] = $object->has_extension('Versioned'); } if ($this->isVersioned[$className]) { $args = $field->options; if (!isset($args['publish']) || $args['publish']) { $this->batchWriter->writeToStage($object, 'Stage', 'Live'); } else { $this->batchWriter->writeToStage($object, 'Stage'); } } else { $this->batchWriter->write($object); } $isSeeded = $object->isSeeded(); if (!$isSeeded) { $isRoot = $field->fieldType === Field::FT_ROOT; $dataObjectProperty = $this->dataObjectRecordProperty; $batchWriter = $this->batchWriter; $object->onAfterExistsCallback(function ($object) use($field, $isRoot, $dataObjectProperty, $batchWriter) { $seed = \QuickDataObject::create('SeedRecord'); $objectFields = $dataObjectProperty->getValue($object); $fields = $dataObjectProperty->getValue($seed); $fields['SeedClassName'] = $object->class; $fields['SeedID'] = $objectFields['ID']; $fields['Key'] = $field->key; $fields['Root'] = $isRoot; $dataObjectProperty->setValue($seed, $fields); $batchWriter->write($seed); }); $object->setIsSeeded(); } }
/** * */ public function testWriteToStages_ManyPages_WritesObjectsToStage() { $sizes = array(10, 30, 100, 300); foreach ($sizes as $size) { $writer = new \BatchedWriter($size); $pages = array(); for ($i = 0; $i < 100; $i++) { $page = new DogPage(); $page->Title = 'Wonder Pup ' . $i; $pages[] = $page; } $writer->writeToStage($pages, 'Stage'); $writer->finish(); $currentStage = \Versioned::current_stage(); \Versioned::reading_stage('Stage'); $this->assertEquals(100, DogPage::get()->Count()); \Versioned::reading_stage('Live'); $this->assertEquals(0, DogPage::get()->Count()); $writer->writeToStage($pages, 'Live'); $writer->finish(); \Versioned::reading_stage('Live'); $this->assertEquals(100, DogPage::get()->Count()); \Versioned::reading_stage($currentStage); $writer->deleteFromStage($pages, 'Stage', 'Live'); $writer->finish(); $this->assertEquals(0, DogPage::get()->Count()); } }