예제 #1
0
 public function indexAction()
 {
     $this->view->setTitle('Карты миграции данных');
     if (!empty($_POST['type'])) {
         foreach ($_POST['type'] as $pathId => $objectType) {
             $mapPath = Migrations\Migration\Map\Path::get($pathId);
             if (is_numeric($objectType)) {
                 $mapPath->object_id = $objectType;
                 $mapPath->type = 'object';
             } else {
                 if ($objectType == 'object') {
                     $object = new Migrations\Migration\Object();
                     $object->model = !empty($_POST['typeOptions'][$pathId]) ? $_POST['typeOptions'][$pathId] : '';
                     $object->migration_id = $mapPath->map->migration_id;
                     $object->code = $object->name = $mapPath->item;
                     $object->save();
                     $mapPath->type = 'object';
                     $mapPath->object_id = $object->id;
                 } else {
                     $mapPath->type = $objectType;
                 }
             }
             $mapPath->save();
         }
     }
     if (!empty($_POST['param'])) {
         foreach ($_POST['param'] as $paramId => $type) {
             $param = \Migrations\Migration\Object\Param::get($paramId);
             if ($type == 'newObject') {
                 $object = new Migrations\Migration\Object();
                 $object->model = !empty($_POST['paramOptions'][$paramId]) ? $_POST['paramOptions'][$paramId] : '';
                 $object->migration_id = $param->object->migration_id;
                 $object->code = $object->name = $param->code;
                 $object->save();
                 $param->type = 'object';
                 $param->value = $object->id;
             } else {
                 $param->type = $type;
                 $param->value = !empty($_POST['paramOptions'][$paramId]) ? $_POST['paramOptions'][$paramId] : '';
             }
             $param->save();
         }
     }
     $models = $this->modules->getSelectListModels();
     $map = \Migrations\Migration\Map::get($_GET['item_pk']);
     $objects = $map->migration->objects(['forSelect' => true]);
     $this->view->page(['data' => compact('map', 'models', 'objects')]);
 }
예제 #2
0
파일: Relation.php 프로젝트: krvd/cms-Inji
 public function parse()
 {
     $options = $this->param->options ? json_decode($this->param->options, true) : [];
     $modelName = $this->object->object->model;
     $relation = $modelName::getRelation($this->param->value);
     $object = \Migrations\Migration\Object::get([['model', $relation['model']], ['migration_id', $this->object->object->migration_id]]);
     if (!$object) {
         $object = new \Migrations\Migration\Object(['model' => $relation['model'], 'code' => $this->param->code, 'name' => $this->param->code, 'migration_id' => $this->object->object->migration_id]);
         $object->save();
     }
     if (!empty($relation['type']) && $relation['type'] == 'many') {
         $ids = [];
         if ($this->data) {
             foreach ($this->data as $code => &$item) {
                 if (!\Tools::isAssoc($this->data)) {
                     foreach ($this->data as &$item) {
                         $objectParser = new \Migrations\Parser\Object();
                         $objectParser->object = $object;
                         $objectParser->parentObject = $this->object;
                         $objectParser->parentModel = $this->model;
                         $objectParser->walker = $this->object->walker;
                         $objectParser->parentParam = $this;
                         $objectParser->data =& $item;
                         if (!$this->model->pk()) {
                             $this->model->save();
                         }
                         $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()]));
                     }
                 } else {
                     $objectParser = new \Migrations\Parser\Object();
                     $objectParser->object = $object;
                     $objectParser->parentObject = $this->object;
                     $objectParser->parentModel = $this->model;
                     $objectParser->walker = $this->object->walker;
                     $objectParser->parentParam = $this;
                     $objectParser->data =& $item;
                     if (!$this->model->pk()) {
                         $this->model->save();
                     }
                     $ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()]));
                 }
             }
         }
         if (!empty($options['clearMissing']) && $this->model->pk()) {
             $where = [];
             $where[] = [$relation['col'], $this->model->pk()];
             if ($ids) {
                 $where[] = ['id', implode(',', $ids), 'NOT IN'];
             }
             $modelName = $relation['model'];
             $objects = $modelName::getList(['where' => $where]);
             foreach ($objects as $delObject) {
                 $objectId = \App::$cur->migrations->findParse($delObject->id, get_class($delObject));
                 if ($objectId) {
                     unset(\App::$cur->migrations->ids['objectIds'][get_class($delObject)][$delObject->id]);
                     unset(\App::$cur->migrations->ids['parseIds'][get_class($delObject)][$objectId->parse_id]);
                     $objectId->delete();
                 }
                 $delObject->delete();
             }
         }
     } else {
         $objectParser = new \Migrations\Parser\Object();
         $objectParser->object = $object;
         $objectParser->parentObject = $this->object;
         $objectParser->parentModel = $this->model;
         $objectParser->parentParam = $this;
         $objectParser->data =& $this->data;
         $ids = [];
         if (!\Tools::isAssoc($this->data)) {
             foreach ($this->data as &$data) {
                 $model = $objectParser->setModel($this->data);
                 if ($model && $model->id) {
                     $ids[] = $model->id;
                 }
             }
         } else {
             $model = $objectParser->setModel($this->data);
             if ($model && $model->id) {
                 $ids[] = $model->id;
             }
         }
         if ($ids) {
             $this->model->{$relation['col']} = $ids[0];
         }
     }
 }