/** * Action to edit a post * * When called via GET, it shows an edit form * including the current data of the Post. * When called via POST, it modifies the post in the * database. * * The expected HTTP parameters are: * <ul> * <li>id: Id of the post (via HTTP POST and GET)</li> * <li>title: Title of the post (via HTTP POST)</li> * <li>content: Content of the post (via HTTP POST)</li> * </ul> * * The views are: * <ul> * <li>posts/edit: If this action is reached via HTTP GET (via include)</li> * <li>posts/index: If post was successfully edited (via redirect)</li> * <li>posts/edit: If validation fails (via include). Includes these view variables:</li> * <ul> * <li>post: The current Post instance, empty or being added (but not validated)</li> * <li>errors: Array including per-field validation errors</li> * </ul> * </ul> * @throws Exception if no id was provided * @throws Exception if no user is in session * @throws Exception if there is not any post with the provided id * @throws Exception if the current logged user is not the author of the post * @return void */ public function edit() { if (!isset($_REQUEST["id"])) { throw new Exception("A post id is mandatory"); } if (!isset($this->currentUser)) { throw new Exception("Not in session. Editing posts requires login"); } // Get the Post object from the database $postid = $_REQUEST["id"]; $post = $this->postMapper->findById($postid); // Does the post exist? if ($post == NULL) { throw new Exception("no such post with id: " . $postid); } // Check if the Post author is the currentUser (in Session) if ($post->getAuthor() != $this->currentUser) { throw new Exception("logged user is not the author of the post id " . $postid); } if (isset($_POST["submit"])) { // reaching via HTTP Post... // populate the Post object with data form the form $post->setTitle($_POST["title"]); $post->setContent($_POST["content"]); try { // validate Post object $post->checkIsValidForUpdate(); // if it fails, ValidationException // update the Post object in the database $this->postMapper->update($post); // POST-REDIRECT-GET // Everything OK, we will redirect the user to the list of posts // We want to see a message after redirection, so we establish // a "flash" message (which is simply a Session variable) to be // get in the view after redirection. $this->view->setFlash(sprintf(i18n("Post \"%s\" successfully updated."), $post->getTitle())); // perform the redirection. More or less: // header("Location: index.php?controller=posts&action=index") // die(); $this->view->redirect("posts", "index"); } catch (ValidationException $ex) { // Get the errors array inside the exepction... $errors = $ex->getErrors(); // And put it to the view as "errors" variable $this->view->setVariable("errors", $errors); } } // Put the Post object visible to the view $this->view->setVariable("post", $post); // render the view (/view/posts/add.php) $this->view->render("posts", "edit"); }
<?php include '../config.php'; include 'orm_config.php'; #include('PostMapper.php'); #include('UserMapper.php'); #include('DomainObjects.php'); $mapper = new PostMapper($db); $post = new Post(); $post->title = 'New Title'; $post->content = 'New content...'; $post->author_id = 1; //$mapper->insert($post); $post->id = 2; $mapper->update($post); $post = $mapper->find(1); $post->title = 'Updated Title'; $post->content = 'Updated content...'; $mapper->update($post); $posts = $mapper->find(); $userMapper = new UserMapper($db); $user = $userMapper->find(1); p($user); ?> <?php foreach ($posts as $post) { ?> <h1><?php echo $post->title; ?>