/** * Ver 3: Change table name from QubitActorName to QubitOtherName and column * actor_id -> object_id * * @return QubitMigrate108 this object */ protected function moveActorNameToOtherName() { if (isset($this->data['QubitActorName'])) { foreach ($this->data['QubitActorName'] as $key => $actorName) { $newKey = str_replace('QubitActorName', 'QubitOtherName', $key); $otherNames[$newKey] = $actorName; // Change actor_id foreign key -> object_id $otherNames[$newKey]['object_id'] = $actorName['actor_id']; unset($otherNames[$newKey]['actor_id']); } // Insert 'QubitOtherName' in same position in data as 'QubitActorName' $insertPos = array_search('QubitActorName', array_keys($this->data)); QubitMigrate::array_insert($this->data, $insertPos, array('QubitOtherName' => $otherNames)); // Remove QubitActorName array unset($this->data['QubitActorName']); } return $this; }
/** * Sort information objects by lft value so that parent objects are inserted * before their children. * * @return QubitMigrate104 this object */ protected function sortQubitInformationObjects() { $newList = array(); $highLft = 0; foreach ($this->data['QubitInformationObject'] as $key => $row) { // If this left value is higher than any previous value, then just add // current row to the end of $newList if ($row['lft'] > $highLft) { $newList[$key] = $row; $highLft = $row['lft']; } else { $i = 0; foreach ($newList as $newKey => $newRow) { if ($newRow['lft'] > $row['lft']) { QubitMigrate::array_insert($newList, $i, array($key => $row)); break; } $i++; } } } $this->data['QubitInformationObject'] = $newList; }
/** * Insert a non-hierarchical $newData into an existing dataset ($originalData), * which contains nested set columns (but is also non-hierarchical in * structure), before the row specified by $pivotKey. Update lft and rgt * values appropriately. * * @param array $originalData The existing YAML dataset array * @param string $pivotKey key of row that should follow the inserted data * @param array $newData data to insert in $originalData */ protected static function insertBeforeNestedSet(array &$originalData, $pivotKey, array $newData) { // If pivotKey doesn't exist, then just return a simple array merge if (!isset($originalData[$pivotKey])) { return array_merge($originalData, $newData); } $pivotIndex = null; $pivotLft = null; $width = count($newData) * 2; // Get index ($i) of pivot row and it's left value (if any) $i = 0; foreach ($originalData as $key => $row) { if ($pivotKey == $key) { $pivotIndex = $i; if (isset($originalData[$key]['lft'])) { $pivotLft = $originalData[$key]['lft']; } break; } $i++; } // If a left value was found, then set merged values for lft & rgt columns if (null !== $pivotIndex) { // Loop through $newData and assign lft & rgt values $j = 0; foreach ($newData as &$row) { $row['lft'] = $pivotLft + $j * 2; $row['rgt'] = $pivotLft + $j * 2 + 1; $j++; } // Bump existing lft & rgt values foreach ($originalData as &$row) { if (isset($row['lft']) && $pivotLft <= $row['lft']) { $row['lft'] += $width; } if (isset($row['rgt']) && $pivotLft < $row['rgt']) { $row['rgt'] += $width; } } } // Merge $newData into $originalData QubitMigrate::array_insert($originalData, $i, $newData); }