GitHub PullRequestReviewCommentEvent at 2014-04-22 13:13:54
Repository: ZF2_3_QuickStart_Rewrite
Actor: manuakasam
Payload: {"comment":{"url":"https://api.github.com/repos/manuakasam/ZF2_3_QuickStart_Rewrite/pulls/comments/11851351","id":11851351,"diff_hunk":"@@ -0,0 +1,727 @@\n+Editting and Deleting Data\n+==========================\n+\n+In the previous chapter we've come to learn how we can use the `Zend\\Form`- and `Zend\\Db`-components to create the\n+functionality of creating new data-sets. This chapter will focus on finalizing the CRUD functionality by introducing\n+the concepts for editting and deleting data. We start by editting the data.\n+\n+\n+Binding Objects to Forms\n+========================\n+\n+The one fundamental difference between an insert- and an edit-form is the fact that inside an edit-form there is\n+already data preset. This means we need to find a way to get data from our database into the form. Luckily `Zend\\Form`\n+provides us with a very handy way of doing so and it's called **data-binding**.\n+\n+All you need to do when providing an edit-form is to get the object of interest from your service and `bind` it to the\n+form. This is done the following way inside your controller.\n+\n+```php\n+<?php\n+// Filename: /module/Album/src/Album/Controller/WriteController.php\n+namespace Album\\Controller;\n+\n+use Album\\Service\\AlbumServiceInterface;\n+use Zend\\Form\\FormInterface;\n+use Zend\\Mvc\\Controller\\AbstractActionController;\n+use Zend\\View\\Model\\ViewModel;\n+\n+class WriteController extends AbstractActionController\n+{\n+    protected $albumService;\n+\n+    protected $albumForm;\n+\n+    public function __construct(\n+        AlbumServiceInterface $albumService,\n+        FormInterface $albumForm\n+    ) {\n+        $this->albumService = $albumService;\n+        $this->albumForm    = $albumForm;\n+    }\n+\n+    public function addAction()\n+    {\n+        $request = $this->getRequest();\n+\n+        if ($request->isPost()) {\n+            $this->albumForm->setData($request->getPost());\n+\n+            if ($this->albumForm->isValid()) {\n+                try {\n+                    $this->albumService->saveAlbum($this->albumForm->getData());\n+\n+                    return $this->redirect()->toRoute('album');\n+                } catch (\\Exception $e) {\n+                    die($e->getMessage());\n+                    // Some DB Error happened, log it and let the user know\n+                }\n+            }\n+        }\n+\n+        return new ViewModel(array(\n+            'form' => $this->albumForm\n+        ));\n+    }\n+\n+    public function editAction()\n+    {\n+        $request = $this->getRequest();\n+        $album   = $this->albumService->findAlbum($this->params('id'));","path":"editting_and_deleting_data.md","position":70,"original_position":70,"commit_id":"9dfb6ff892301455405719cfc25c87e02f906bf4","original_commit_id":"9dfb6ff892301455405719cfc25c87e02f906bf4","user":{"login":"manuakasam","id":1087280,"avatar_url":"https://avatars.githubusercontent.com/u/1087280?","gravatar_id":"b6fdce589f9a5243a48d541d7444900c","url":"https://api.github.com/users/manuakasam","html_url":"https://github.com/manuakasam","followers_url":"https://api.github.com/users/manuakasam/followers","following_url":"https://api.github.com/users/manuakasam/following{/other_user}","gists_url":"https://api.github.com/users/manuakasam/gists{/gist_id}","starred_url":"https://api.github.com/users/manuakasam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manuakasam/subscriptions","organizations_url":"https://api.github.com/users/manuakasam/orgs","repos_url":"https://api.github.com/users/manuakasam/repos","events_url":"https://api.github.com/users/manuakasam/events{/privacy}","received_events_url":"https://api.github.com/users/manuakasam/received_events","type":"User","site_admin":false},"body":":thumbsup: can be added as a note under the route definition below.","created_at":"2014-04-22T13:13:54Z","updated_at":"2014-04-22T13:13:54Z","html_url":"https://github.com/manuakasam/ZF2_3_QuickStart_Rewrite/pull/16#discussion_r11851351","pull_request_url":"https://api.github.com/repos/manuakasam/ZF2_3_QuickStart_Rewrite/pulls/16","_links":{"self":{"href":"https://api.github.com/repos/manuakasam/ZF2_3_QuickStart_Rewrite/pulls/comments/11851351"},"html":{"href":"https://github.com/manuakasam/ZF2_3_QuickStart_Rewrite/pull/16#discussion_r11851351"},"pull_request":{"href":"https://api.github.com/repos/manuakasam/ZF2_3_QuickStart_Rewrite/pulls/16"}}}}