public function actionShow($id) { $this->good = $this->goods->get($id); if (!$this->good) { $this->flashMessage('Výrobek se nepodařilo načíst.', 'danger'); $this->redirect(':Front:Catalog:default'); } $goodsSimilar = array(); $goodsSameManufacturer = $this->goods->createSelectionInstance()->where("manufacturer_id = ?", $this->good->manufacturer->id)->order('id DESC')->limit(2); $goodCG = $this->good->related('category_goods'); $goodCategoryIds = array(); foreach ($goodCG as $cg) { $goodCategoryIds[] = $cg->category->id; } $goodsSimilarCG = $this->categoryGoods->createSelectionInstance()->where('category_id IN ?', $goodCategoryIds); foreach ($goodsSimilarCG as $cg) { if ($cg->good->id == $this->good->id) { continue; } $goodsSimilar[] = $cg->good; } foreach ($goodsSameManufacturer as $good) { if ($good->id == $this->good->id) { continue; } $goodsSimilar[] = $good; } $this->goodsSimilar = array_unique($goodsSimilar); }
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); }