/** * POST: Create new object if no id is presented, otherwise update the object with given id * @param Request $request * @return Response */ protected function createOrUpdate(Request $request) { $object = Api::handle($request, $this->getDefaultRepositoryName()); EntityManager::persist($object); EntityManager::flush(); return Api::render($object, $this->getDefaultDetailsSerializerGroup()); }
/** * Create new post if the id is given otherwise update it * * @param Request $request * @param integer $id * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function createOrEdit(Request $request, $id = null) { $post = $id ? $this->getRepository()->find($id) : new Post(); $form = $this->getForm($post, $id != null); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $obj = $form->getData(); EntityManager::persist($obj); EntityManager::flush(); return Redirect::route('post.list'); } return view('postDetails', ['form' => $form->createView()]); }
/** * @param string $prefix * @return array */ protected function _saveTags($prefix = 'testTag#0') { $aTags = [$prefix . '-1', $prefix . '-2', $prefix . '-3']; $savedTags = []; foreach ($aTags as $tagName) { $tagEntity = new Tag($tagName); EntityManager::persist($tagEntity); $savedTags[] = $tagEntity; } EntityManager::flush(); return array_map(function (Tag $oTag) { return $oTag->getId(); }, $savedTags); }
/** * Store a newly created resource in storage. * * @return \Illuminate\Http\Response */ public function store() { $input = Request::all(); $article = $this->repository->createOrUpdate($input); // $article->addTag(new Tag('newTag')); EntityManager::persist($article); EntityManager::flush(); if ($article->getId()) { return redirect(url("articles", [$article->getId()])); } return Redirect::back()->withInput(); }