Пример #1
0
 private function parseData($data, $preset)
 {
     $model = $this->setModel($data);
     if ($model) {
         foreach ($preset as $col => $value) {
             $model->{$col} = $value;
         }
         $walked = [];
         foreach ($this->object->params as $param) {
             if ($model && $param->type && $param->type != 'item_key') {
                 if ($param->type == 'object') {
                     $object = \App::$cur->migrations->getMigrationObject($param->value);
                     $parser = new \Migrations\Parser\Object();
                     $parser->data =& $data[$param->code];
                     $parser->object = $object;
                     $parser->parentObject = $this;
                     $parser->parentModel = $model;
                     $parser->walker = $this->walker;
                     $parser->parse();
                 } else {
                     if ($param->type == 'custom') {
                         $parserName = $param->value;
                     } else {
                         $parserName = '\\Migrations\\Parser\\Object\\' . ucfirst($param->type);
                     }
                     $parser = new $parserName();
                     $parser->data =& $data[$param->code];
                     $parser->param = $param;
                     $parser->model = $model;
                     $parser->object = $this;
                     $parser->parse();
                 }
             }
             $walked[$param->code] = true;
         }
         //check unparsed params
         foreach ($data as $key => $item) {
             //skip parsed and attribtes
             if ($key == '@attributes' || !empty($walked[$key])) {
                 continue;
             }
             $param = new \Migrations\Migration\Object\Param();
             $param->object_id = $this->object->id;
             $param->code = $key;
             $param->save();
         }
         if ($model) {
             if (!$model->pk() || !empty($model->_changedParams)) {
                 $model->save();
             }
             return $model->pk();
         }
     }
     return 0;
 }
Пример #2
0
 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];
         }
     }
 }