/** * Test Hierarchy::AllHistoricalChildren(). */ public function testAllHistoricalChildren() { // Delete some objs $this->objFromFixture('HierarchyTest_Object', 'obj2b')->delete(); $this->objFromFixture('HierarchyTest_Object', 'obj3a')->delete(); $this->objFromFixture('HierarchyTest_Object', 'obj3')->delete(); // Check that obj1-3 appear at the top level of the AllHistoricalChildren tree $this->assertEquals(array("Obj 1", "Obj 2", "Obj 3"), singleton('HierarchyTest_Object')->AllHistoricalChildren()->column('Title')); // Check numHistoricalChildren $this->assertEquals(3, singleton('HierarchyTest_Object')->numHistoricalChildren()); // Check that both obj 2 children are returned $obj2 = $this->objFromFixture('HierarchyTest_Object', 'obj2'); $this->assertEquals(array("Obj 2a", "Obj 2b"), $obj2->AllHistoricalChildren()->column('Title')); // Check numHistoricalChildren $this->assertEquals(2, $obj2->numHistoricalChildren()); // Obj 3 has been deleted; let's bring it back from the grave $obj3 = Versioned::get_including_deleted("HierarchyTest_Object", "\"Title\" = 'Obj 3'")->First(); // Check that all obj 3 children are returned $this->assertEquals(array("Obj 3a", "Obj 3b", "Obj 3c", "Obj 3d"), $obj3->AllHistoricalChildren()->column('Title')); // Check numHistoricalChildren $this->assertEquals(4, $obj3->numHistoricalChildren()); }
/** * Return all the children that this page had, including pages that were deleted from both stage & live. * * @return DataList * @throws Exception */ public function AllHistoricalChildren() { if (!$this->owner->hasExtension('SilverStripe\\ORM\\Versioning\\Versioned')) { throw new Exception('Hierarchy->AllHistoricalChildren() only works with Versioned extension applied'); } $baseTable = $this->owner->baseTable(); $parentIDColumn = $this->owner->getSchema()->sqlColumnForField($this->owner, 'ParentID'); return Versioned::get_including_deleted($this->owner->baseClass(), [$parentIDColumn => $this->owner->ID], "\"{$baseTable}\".\"ID\" ASC"); }
/** * Safely query and return all pages queried * * @param array $ids * @return SS_List */ protected function getPages($ids) { // Check empty set if (empty($ids)) { return new ArrayList(); } $recordClass = $this->recordClass; // Bypass translatable filter if (class_exists('Translatable') && $recordClass::has_extension('Translatable')) { Translatable::disable_locale_filter(); } // Bypass versioned filter if ($recordClass::has_extension(Versioned::class)) { // Workaround for get_including_deleted not supporting byIDs filter very well // Ensure we select both stage / live records $pages = Versioned::get_including_deleted($recordClass, array('"RecordID" IN (' . DB::placeholders($ids) . ')' => $ids)); } else { $pages = DataObject::get($recordClass)->byIDs($ids); } if (class_exists('Translatable') && $recordClass::has_extension('Translatable')) { Translatable::enable_locale_filter(); } return $pages; }
/** * Test Versioned::get_including_deleted() */ public function testGetIncludingDeleted() { // Get all ids of pages $allPageIDs = DataObject::get('VersionedTest_DataObject', "\"ParentID\" = 0", "\"VersionedTest_DataObject\".\"ID\" ASC")->column('ID'); // Modify a page, ensuring that the Version ID and Record ID will differ, // and then subsequently delete it $targetPage = $this->objFromFixture('VersionedTest_DataObject', 'page3'); $targetPage->Content = 'To be deleted'; $targetPage->write(); $targetPage->delete(); // Get all items, ignoring deleted $remainingPages = DataObject::get("VersionedTest_DataObject", "\"ParentID\" = 0", "\"VersionedTest_DataObject\".\"ID\" ASC"); // Check that page 3 has gone $this->assertNotNull($remainingPages); $this->assertEquals(array("Page 1", "Page 2", "Subclass Page 1"), $remainingPages->column('Title')); // Get all including deleted $allPages = Versioned::get_including_deleted("VersionedTest_DataObject", "\"ParentID\" = 0", "\"VersionedTest_DataObject\".\"ID\" ASC"); // Check that page 3 is still there $this->assertEquals(array("Page 1", "Page 2", "Page 3", "Subclass Page 1"), $allPages->column('Title')); // Check that the returned pages have the correct IDs $this->assertEquals($allPageIDs, $allPages->column('ID')); // Check that this still works if we switch to reading the other stage Versioned::set_stage(Versioned::LIVE); $allPages = Versioned::get_including_deleted("VersionedTest_DataObject", "\"ParentID\" = 0", "\"VersionedTest_DataObject\".\"ID\" ASC"); $this->assertEquals(array("Page 1", "Page 2", "Page 3", "Subclass Page 1"), $allPages->column('Title')); // Check that the returned pages still have the correct IDs $this->assertEquals($allPageIDs, $allPages->column('ID')); }