public function index(string $_mode, $_models, RouteEx $_route, array $_parents, string $alias)
 {
     if ($_mode === 'delete') {
         if ($parent = $this->findParentByAlias($_parents, $alias)) {
             $cascade = $_route->getDeleteCascades();
             $cascades = $cascade[$parent['alias']];
             /** @var ModelEx $model */
             foreach ($_models as $model) {
                 $pk = $model->getKeyName();
                 $pkValue = $model->{$pk};
                 if (!empty($cascades)) {
                     foreach ((array) $cascades as $child) {
                         if ($child = $this->findParentByAlias($_parents, $child)) {
                             $name = $child['name'];
                             if ($childModel = $this->resolver->getModel($name)) {
                                 $records = $childModel::where($pk, '=', $pkValue)->get();
                                 if ($recursive = $cascade[$child['alias']] ?? null) {
                                     $this->index($_mode, $records, $_route, $_parents, $child['alias']);
                                 }
                                 /** @var ModelEx $record */
                                 foreach ($records as $record) {
                                     $record->delete();
                                 }
                             } else {
                                 throw new ModelError("Cannot find child model '{$name}' for delete cascade");
                             }
                         } else {
                             throw new ModelError("Child model '{$child}' not defined");
                         }
                     }
                 }
                 if ($model->delete()) {
                     $items[] = ['pk' => $pkValue];
                 }
             }
         }
     } else {
         /** @var ModelEx $model */
         foreach ($_models as $model) {
             $this->modelAutoFill->fillMissing($model);
             $model->save();
             $items[] = $model->toArray();
         }
     }
     return json_encode(['items' => $items ?? []]);
 }
Example #2
0
 public function index(RouteEx $_route)
 {
     $viewPath = Str::camel(str_replace(' ', '', ucwords(preg_replace('/(\\W+)/', '\\1 ', $_route->getPath()))));
     return new View($viewPath, [], false);
 }
Example #3
0
 public function index(RouteEx $_route)
 {
     $viewPath = $this->getViewPath($_route->getPath());
     return new View($viewPath);
 }