$_POST['name'], Step::$TEXT => $_POST['text'], Step::$VIS => boolval($_POST['vis']), ]; if($step_id){ Step::updateById($step_id,$data ); } else { $r = Step::insert($data); $step_id = $r->id; } foreach($sections as $id=>$v){ if(!$id) continue; if($id<0){ $r = Section::insert([ Section::$NAME => $v['name'], Section::$STEP => $step_id, ]); $sections[$id]['id'] = $r->id; } else { Section::updateById($id,[ Section::$NAME=>$v['name'] ]); } } foreach ($structure as $pid=>$ids){ foreach ($ids as $id){ $parent = $sections[$pid]; if(!$pid or ($parent and intval($pid))) { $real_id = $sections[$id]['id']; Section::updateById($real_id, [ Section::$PARENT => $parent['id'], ]); $IDS[] = $real_id; } } } Section::delete($w = new \Where( \Where::_and([ \Where::_not([ \Where::_in(Section::$ID, $IDS) ]), \Where::_operator(Section::$STEP,'=',$step_id) ]) )); } echo je($res); } }