/**
  * @Route("/write", name="write")
  * @Method({"GET", "POST"})
  */
 public function appshedAction(Request $request)
 {
     $view = $this->getView($request);
     if (!$view->getId()) {
         $screen = new Screen("Error");
         $screen->addChild(new Text("You must setup the view first"));
         return (new Remote($screen))->getSymfonyResponse();
     }
     $store = $view->getStore();
     $data = Remote::getRequestVariables();
     $systemFields = $this->container->getParameter('data_system_fields');
     foreach (array_keys($data) as $key) {
         if (in_array($key, $systemFields)) {
             unset($data[$key]);
             continue;
         }
         if (is_array($data[$key])) {
             $data[$key] = '[' . implode(', ', $data[$key]) . ']';
         }
     }
     $cols = array_keys($data);
     if (!count($cols)) {
         $screen = new Screen("Error");
         $screen->addChild(new Text("You must send some data"));
         return (new Remote($screen))->getSymfonyResponse();
     }
     //Add any new columns to the store
     $newColumns = array_diff($cols, $store->getColumns());
     if (count($newColumns)) {
         $store->setColumns(array_merge($store->getColumns(), $newColumns));
     }
     //Create new data obj
     $dataO = new Data();
     $dataO->setColumns($cols);
     $dataO->setData($data);
     $dataO->setStore($store);
     $em = $this->getDoctrine()->getManager();
     $em->persist($dataO);
     $em->flush();
     $screen = new Screen($view->getTitle() ?: "Thank you");
     $screen->addChild(new Text($view->getMessage() ?: "Your entry has been saved"));
     return (new Remote($screen))->getSymfonyResponse();
 }
 private function insertData(Api $api, Request $request)
 {
     //get this data here (before getDataForApi()) because $em->clear() make bad
     $storeColumns = $api->getStore()->getColumns();
     $appId = $api->getApp()->getAppId();
     $data = $request->request->all();
     $executed = 0;
     if (empty($data)) {
         throw new MissingDataException('No data given');
     }
     $dataO = new Data();
     $dataO->setStore($api->getStore());
     $dataO->setColumns(array_keys($data));
     $dataO->setData($data);
     $this->getDoctrine()->getManager()->persist($dataO);
     $this->getDoctrine()->getManager()->flush();
     $store = $api->getStore();
     $em = $this->getDoctrine()->getManager();
     //Add any new columns to the store
     $newColumns = array_diff(array_keys($data), $storeColumns);
     if (count($newColumns)) {
         $store->setColumns(array_merge($storeColumns, $newColumns));
     }
     $em->merge($store);
     $em->flush();
     $executed++;
     return ['updatedRows' => $executed];
 }