function extractPath($data, $path = null) { if (empty($path)) { return $data; } if (is_object($data)) { $data = get_object_vars($data); } if (!is_array($path)) { if (!class_exists('String')) { App::import('Core', 'String'); } $parts = String::tokenize($path, '.', '{', '}'); } else { $parts = $path; } if (!is_array($data)) { return array(); } $tmp = array(); if (empty($parts) || !is_array($parts)) { return array(); } $key = reset($parts); $tmpPath = array_slice($parts, 1); if (is_numeric($key) && intval($key) > 0 || $key === '0') { if (isset($data[intval($key)])) { $tmp[intval($key)] = $data[intval($key)]; } else { return array(); } } elseif ($key === '{n}') { foreach ($data as $j => $val) { if (is_int($j)) { $tmp[$j] = $val; } } } elseif ($key === '{s}') { foreach ($data as $j => $val) { if (is_string($j)) { $tmp[$j] = $val; } } } elseif (false !== strpos($key, '{') && false !== strpos($key, '}')) { $pattern = substr($key, 1, -1); foreach ($data as $j => $val) { if (preg_match('/^' . $pattern . '/s', $j) !== 0) { $tmp[$j] = $val; } } } else { if (isset($data[$key])) { $tmp[$key] = $data[$key]; } else { return array(); } } $res = array(); if (!empty($tmpPath)) { foreach ($tmp as $key => $val) { $res2 = Migration::extractPath($val, $tmpPath); foreach ($res2 as $key2 => $val2) { $res[$key . '.' . $key2] = $val2; } } } else { return $tmp; } return $res; }
function prepDataForMigration($exclude = array()) { // $this->LocalModel, $this->entry, $this->targetInstance // $Model, $entry, $targetInstance, $settings = $this->LocalModel->migrationSettings(); $exclude = array_merge($exclude, array($this->LocalModel->primaryKey), $settings['excludeFields']); $fullName = $this->LocalModel->getFullName(); $alias = $this->LocalModel->alias; $entry = $this->entry; $assoc = $this->LocalModel->getMigratedAssociations(); if (!empty($assoc)) { foreach ($assoc as $name => $opt) { $paths = array(); if (!empty($opt['path'])) { $paths = Migration::extractPath($entry[$alias], $opt['path']); } elseif (!empty($opt['foreignKey']) && array_key_exists($opt['foreignKey'], $entry[$alias])) { $paths = array($opt['foreignKey'] => $entry[$alias][$opt['foreignKey']]); } //debug($paths); if (!empty($paths)) { $AssocModel = Migration::getLocalModel($opt['className']); foreach ($paths as $path => $local_id) { $removed = false; $remote_id = $AssocModel->getRemoteId($local_id, $this->targetInstance); if (is_null($remote_id)) { if (isset($opt['unsetLevel'])) { $entry[$alias] = Set::remove($entry[$alias], implode('.', array_slice(explode('.', $path), 0, $opt['unsetLevel']))); $removed = true; } if (!empty($opt['autoTrack'])) { $entry['MigrationTracking'][$opt['className']][$local_id] = 1; } if (!empty($opt['autoSelect'])) { $this->Batch->Process->autoSelect[] = array('model' => $opt['className'], 'local_id' => $local_id); } $entry['MigrationMissing'][] = array('model' => $opt['className'], 'local_id' => $local_id); } if (!$removed) { $entry[$alias] = Set::insert($entry[$alias], $path, $remote_id); } } } } //debug($assoc); } //debug($entry[$alias]); $raw = $this->LocalModel->getRawEntry($entry); $data = $raw[$alias]; $data = array_diff_key($data, array_flip($exclude)); $entry['MigrationData'] = $data; return $entry; }