|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Parser Object Relation |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Migrations\Parser\Object; |
|
13
|
|
|
|
|
14
|
|
|
class Relation extends \Migrations\Parser |
|
15
|
|
|
{ |
|
16
|
|
|
public function parse() |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
$options = $this->param->options ? json_decode($this->param->options, true) : []; |
|
20
|
|
|
$modelName = $this->object->object->model; |
|
21
|
|
|
$relation = $modelName::getRelation($this->param->value); |
|
22
|
|
|
$object = \Migrations\Migration\Object::get([ |
|
23
|
|
|
['model', $relation['model']], |
|
24
|
|
|
['migration_id', $this->object->object->migration_id] |
|
25
|
|
|
]); |
|
26
|
|
|
if (!$object) { |
|
27
|
|
|
$object = new \Migrations\Migration\Object([ |
|
28
|
|
|
'model' => $relation['model'], |
|
29
|
|
|
'code' => $this->param->code, |
|
30
|
|
|
'name' => $this->param->code, |
|
31
|
|
|
'migration_id' => $this->object->object->migration_id |
|
32
|
|
|
]); |
|
33
|
|
|
$object->save(); |
|
34
|
|
|
} |
|
35
|
|
|
if (!empty($relation['type']) && $relation['type'] == 'many') { |
|
36
|
|
|
$ids = []; |
|
37
|
|
|
|
|
38
|
|
|
if ($this->data) { |
|
39
|
|
|
foreach ($this->data as $code => &$item) { |
|
40
|
|
|
if (!\Tools::isAssoc($this->data)) { |
|
41
|
|
|
foreach ($this->data as &$item) { |
|
42
|
|
|
$objectParser = new \Migrations\Parser\Object(); |
|
43
|
|
|
$objectParser->object = $object; |
|
44
|
|
|
$objectParser->parentObject = $this->object; |
|
45
|
|
|
$objectParser->parentModel = $this->model; |
|
46
|
|
|
$objectParser->walker = $this->object->walker; |
|
47
|
|
|
$objectParser->parentParam = $this; |
|
48
|
|
|
$objectParser->data = &$item; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (!$this->model->pk()) { |
|
52
|
|
|
$this->model->save(); |
|
53
|
|
|
} |
|
54
|
|
|
$ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()])); |
|
55
|
|
|
} |
|
56
|
|
|
} else { |
|
57
|
|
|
$objectParser = new \Migrations\Parser\Object(); |
|
58
|
|
|
$objectParser->object = $object; |
|
59
|
|
|
$objectParser->parentObject = $this->object; |
|
60
|
|
|
$objectParser->parentModel = $this->model; |
|
61
|
|
|
$objectParser->walker = $this->object->walker; |
|
62
|
|
|
$objectParser->parentParam = $this; |
|
63
|
|
|
$objectParser->data = &$item; |
|
64
|
|
|
if (!$this->model->pk()) { |
|
65
|
|
|
$this->model->save(); |
|
66
|
|
|
} |
|
67
|
|
|
$ids = array_merge($ids, $objectParser->parse([$relation['col'] => $this->model->pk()])); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
if (!empty($options['clearMissing']) && $this->model->pk()) { |
|
72
|
|
|
$where = []; |
|
73
|
|
|
$where[] = [$relation['col'], $this->model->pk()]; |
|
74
|
|
|
if ($ids) { |
|
75
|
|
|
$where[] = ['id', implode(',', $ids), 'NOT IN']; |
|
76
|
|
|
} |
|
77
|
|
|
$modelName = $relation['model']; |
|
78
|
|
|
$objects = $modelName::getList(['where' => $where]); |
|
79
|
|
|
foreach ($objects as $delObject) { |
|
80
|
|
|
$objectId = \App::$cur->migrations->findParse($delObject->id, get_class($delObject)); |
|
81
|
|
|
if ($objectId) { |
|
82
|
|
|
unset(\App::$cur->migrations->ids['objectIds'][get_class($delObject)][$delObject->id]); |
|
83
|
|
|
unset(\App::$cur->migrations->ids['parseIds'][get_class($delObject)][$objectId->parse_id]); |
|
84
|
|
|
$objectId->delete(); |
|
85
|
|
|
} |
|
86
|
|
|
$delObject->delete(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} else { |
|
90
|
|
|
$objectParser = new \Migrations\Parser\Object(); |
|
91
|
|
|
$objectParser->object = $object; |
|
92
|
|
|
$objectParser->parentObject = $this->object; |
|
93
|
|
|
$objectParser->parentModel = $this->model; |
|
94
|
|
|
$objectParser->parentParam = $this; |
|
95
|
|
|
$objectParser->data = &$this->data; |
|
96
|
|
|
$ids = []; |
|
97
|
|
|
if (!\Tools::isAssoc($this->data)) { |
|
98
|
|
|
foreach ($this->data as &$data) { |
|
99
|
|
|
$model = $objectParser->setModel($this->data); |
|
100
|
|
|
if ($model && $model->id) { |
|
101
|
|
|
$ids[] = $model->id; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} else { |
|
105
|
|
|
$model = $objectParser->setModel($this->data); |
|
106
|
|
|
if ($model && $model->id) { |
|
107
|
|
|
$ids[] = $model->id; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
if ($ids) { |
|
111
|
|
|
$this->model->{$relation['col']} = $ids[0]; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|