/** * Action - test/addpost * add post via Active Record * * @return string */ public function addpostAction() { $request = $this->getRequest(); $locale = $this->getLocale(); $format = $locale == 'ru' ? 'd.m.Y' : 'm/d/Y'; $db = $this->app['ar']; //-------------------- try { // Initialization $this->init(__CLASS__ . "/" . __FUNCTION__); // Create object NewPost and set values $ormPost = new Post(); if ($this->isMethod('GET')) { $ormPost->setCreated(date($format)); } // Create form $form = $this->createForm(new PostForm(), $ormPost, array('action' => "/test/addpost")); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // Get username $user = $this->getUser(); $username = $user->getUsername(); // Get user $user = $db->find('user', 'first', array('conditions' => "username='******'")); // Create model post and set values $arPost = $db->create('post', $ormPost->getValues()); $arPost->user_id = $user->id; $arPost->saveModel(); // Send flash message $this->addFlash('info_message', $this->trans('added_new_message', array('{{ title }}' => $ormPost->getTitle()))); // Go to "/account" return $this->redirect("/account"); } // Show form return $this->showView(array('form' => $form->createView())); } catch (\Exception $exc) { return $this->showError($exc); } }
/** * Action - blog/new * add new post for current user * * @return string */ public function newAction() { $request = $this->getRequest(); $models = $this->app['models']; $locale = $this->getLocale(); $format = $locale == 'ru' ? 'd.m.Y' : 'm/d/Y'; //-------------------- try { // Initialization $this->init(__CLASS__ . "/" . __FUNCTION__); // Create object NewPost and set values $ormPost = new Post(); $ormPost->setCreated(date($format)); // Create form $form = $this->createForm(new PostForm(), $ormPost, array('action' => "/blog/new")); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $user = $this->getUser(); $username = $user->getUsername(); // Create new post $models->load('Post', 'newPost', array('username' => $username, 'new_post' => $ormPost)); $this->addFlash('info_message', $this->trans('added_new_message', array('{{ title }}' => $ormPost->getTitle()))); return $this->redirect("/account"); } // Show form return $this->showView(array('form' => $form->createView())); } catch (\Exception $exc) { return $this->showError($exc); } }