public function itemFormSucceeded(UI\Form $form, $values) { if (isset($values['manufacturer_id']) && !$values['manufacturer_id']) { $values['manufacturer_id'] = $values['manufacturer_id'] ? $values['manufacturer_id'] : null; } foreach ($values as $k => $v) { if ($k != 'category_id' && $k != 'photo') { $updateOrInsert[$k] = $v; } } $itemId = $this->getParameter('id'); if ($itemId) { $item = $this->item->get($itemId); if (!$item) { $this->error('Data nebyla nalezena v databázi.', 404); } else { $item->update($updateOrInsert); } $this->categoryGoods->where('goods_id', $itemId)->delete(); $forInsert = array(); foreach ($values['category_id'] as $categoryId) { $forInsert[] = ['category_id' => $categoryId, 'goods_id' => $itemId]; } $this->categoryGoods->insert($forInsert); $this->flashMessage('Změny uloženy.', 'success'); } else { $item = $this->item->insert($updateOrInsert); // categories $forInsert = array(); foreach ($values['category_id'] as $categoryId) { $forInsert[] = ['category_id' => $categoryId, 'goods_id' => $item->id]; } $this->categoryGoods->insert($forInsert); $this->flashMessage('Výrobek vložen do databáze.', 'success'); } if ($item && $values['photo']) { if ($this->photo->insertPhoto($values['photo'], $item->id)) { $this->flashMessage('Byl přiložen obrázek.', 'success'); } } $this->redirect('edit', $item->id); }