GitHub PullRequestReviewCommentEvent at 2014-02-07 02:16:29
Repository: board_matsukawa
Actor: gomo
Payload: {"comment":{"url":"https://api.github.com/repos/SunriseDigitalStudy/board_matsukawa/pulls/comments/9527974","id":9527974,"diff_hunk":"@@ -0,0 +1,152 @@\n+<?php\n+\n+/*\n+ * To change this license header, choose License Headers in Project Properties.\n+ * To change this template file, choose Tools | Templates\n+ * and open the template in the editor.\n+ */\n+\n+/**\n+ * Description of AjaxController\n+ *\n+ * @author Matsukawa\n+ */\n+class JsonController extends Sdx_Controller_Action_Http {\n+\n+  //ページング処理に使う値\n+  const PER_PAGE = 5; //1ページあたりの表示件数\n+\n+  public function indexAction() {\n+    $this->_disableViewRenderer();\n+  }\n+\n+  public function listAction() {\n+\n+    /*\n+     * Sdx_Formで入力フォームを作成\n+     */\n+\n+    $form = new Sdx_Form();\n+    $form\n+            ->setAction(\"javascript:void(0);\") //Action先を現在のURLに指定\n+            ->setMethodToPost()  //メソッドをPOSTに変更\n+            ->setId('form1');\n+\n+    //各フォームにエレメントをセット\n+    //ジャンル選択ラジオボタン\n+    $elems = new Sdx_Form_Element_Group_Radio();\n+    $genre_list = Bd_Orm_Main_Genre::createTable()\n+            ->getSelect()\n+            ->setColumns(array('id', 'name'))\n+            ->fetchPairs();\n+    $elems->setName('genre_id')->addChildren($genre_list);\n+    $form->setElement($elems);\n+\n+    //タグ選択チェックボックス\n+    $elems = new Sdx_Form_Element_Group_Checkbox();\n+    $tag_list = Bd_Orm_Main_Tag::createTable()\n+            ->getselect()\n+            ->setColumns(array('id', 'name'))\n+            ->fetchPairs();\n+    $elems->setName('tag_ids')->addChildren($tag_list);\n+    $form->setElement($elems);\n+\n+    $this->view->assign('form', $form);\n+  }\n+\n+  public function searchAction() {\n+\n+    $this->_disableViewRenderer();\n+\n+    /* -------------------------------------------------------------------------------\n+     *  複合条件検索のSQL文\n+      SELECT *\n+      FROM thread\n+      INNER JOIN thread_tag ON thread.id = thread_tag.thread_id\n+      INNER JOIN tag ON thread_tag.tag_id = tag.id\n+      WHERE\n+      thread_tag.tag_id IN (11, 10)\n+      AND\n+      genre_id = 4\n+      GROUP BY\n+      thread.id HAVING COUNT(tag_id) = 2; //2はチェックボックスのチェックされた数\n+     * ------------------------------------------------------------------------------\n+     */\n+\n+    /*\n+     * 複合条件検索のSQLをORMで作成\n+     */\n+\n+    //絞り込み条件の値(パラメータ)を取得\n+    $genre_id = $this->_getParam('genre_id');\n+    $tag_ids = $this->_getParam('tag_ids');\n+\n+    //並び順用サブクエリの作成\n+    //SELECT thread_id, Max(updated_at) AS updated  FROM entry GROUP BY thread_id\n+    $t_entry = Bd_Orm_Main_Entry::createTable();\n+    $select_en = $t_entry->getSelect();\n+    $select_en->resetColumns()\n+            ->columns('thread_id')\n+            ->columns('Max(updated_at) AS updated')\n+            ->group('thread_id');\n+\n+\n+    //メインクエリ作成\n+    $t_thread = Bd_Orm_Main_Thread::createTable();\n+\n+    //タグ条件絞り込み検索のためのテーブル生成＆join\n+    if ($tag_ids) {\n+      $t_thread_tag = Bd_Orm_Main_ThreadTag::createTable();\n+      $t_thread->addJoinLeft($t_thread_tag);\n+    }\n+\n+    //全件検索\n+    $select_th = $t_thread->getSelectWithJoin();\n+    $sub_Query = $select_th->expr('(' . $select_en->assemble() . ')');\n+    $select_th\n+            ->joinLeft(array('max_updated' => $sub_Query), 'thread.id = max_updated.thread_id')\n+            ->setColumns(array('thread.id','title','max_updated.updated'))\n+            ->order('(CASE WHEN updated is null THEN 1 ELSE 2 END), updated DESC');\n+    //タグ条件で絞込み\n+    if ($tag_ids) {\n+      $select_th\n+              ->add('thread_tag.tag_id', $tag_ids)\n+              ->group('thread.id')\n+              ->having('COUNT(tag_id) =' . count($tag_ids));\n+    }\n+    //ジャンル条件で絞込み\n+    if ($genre_id) {\n+      $select_th\n+              ->add('genre_id', $genre_id);\n+    }\n+\n+    \n+    /*\n+     * ページング処理\n+     */\n+    $count = $select_th->countRow(); //総レコード数\n+    //コンストラクタ内でURLのクエリ文字列から直に、表示するページナンバーを取得している\n+    $sdx_pager = new Sdx_Pager(self::PER_PAGE, $count); \n+    $select_th->limitPager($sdx_pager);\n+\n+    //sql発行\n+    $thread_list = $t_thread->fetchAll($select_th);\n+    \n+    /*\n+     * json出力\n+     */\n+    $json_data = array(\n+                  'thread_list' => $thread_list->toArray(), //データを配列に変換\n+                  'page' => array(  //ページングデータ\n+                              'nextPage' => $sdx_pager->getNextPageId(), \n+                              'prevPage' => $sdx_pager->getPrevPageId())\n+    );\n+    \n+    \n+    $json_data = json_encode($json_data);\n+    header(\"Content_Type: application/json; charset=utf-8\");\n+    echo $json_data;","path":"board.sunrisedigital.jp/app/controllers/default/JsonController.php","position":148,"original_position":148,"commit_id":"b81de71ae4548e8b93d76c6cea6c14a2282e8b8d","original_commit_id":"b81de71ae4548e8b93d76c6cea6c14a2282e8b8d","user":{"login":"gomo","id":639604,"avatar_url":"https://gravatar.com/avatar/4194af7d43b6ba71c3066476b91d508d?d=https%3A%2F%2Fidenticons.github.com%2Fafa01baeef41639dd80e66d47c44b4a8.png&r=x","gravatar_id":"4194af7d43b6ba71c3066476b91d508d","url":"https://api.github.com/users/gomo","html_url":"https://github.com/gomo","followers_url":"https://api.github.com/users/gomo/followers","following_url":"https://api.github.com/users/gomo/following{/other_user}","gists_url":"https://api.github.com/users/gomo/gists{/gist_id}","starred_url":"https://api.github.com/users/gomo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gomo/subscriptions","organizations_url":"https://api.github.com/users/gomo/orgs","repos_url":"https://api.github.com/users/gomo/repos","events_url":"https://api.github.com/users/gomo/events{/privacy}","received_events_url":"https://api.github.com/users/gomo/received_events","type":"User","site_admin":false},"body":"Zend_ControllerのWEB側（CLI側は最近話題に登ったんですけど、ちょっと不明です。調査しておきます）では`echo`などを使って文字列を出力してはいけません。コントローラープラグインなどの機能がうまく動かなくなる可能性があるからです。また、同じような理由で`header `関数も使用禁止です。代わりにResponseオブジェクトを使います。\r\n\r\n```php\r\n$this->getResponse()->setBody($json_data);\r\n$this->getResponse()->setHeader('Content-Type', 'application/json; charset=utf-8');\r\n```\r\n（あと、Content_TypeじゃなくてContent-Typeだけど、これはうまく送れてました？うまく遅れるならうまい具合に何処かのレイヤーが吸収してくれてるんですかね？）\r\n\r\nさらに、jsonで出力は結構回数が多いのでSdx_Controller_Action_Httpにメソッドが有ります。上記処理を書く必要すらありません。\r\n\r\n```php\r\n//$json_dataは配列のままでOK\r\n$this->jsonResponse($json_data);\r\n```\r\n\r\n","created_at":"2014-02-07T02:16:29Z","updated_at":"2014-02-07T02:16:29Z","html_url":"https://github.com/SunriseDigitalStudy/board_matsukawa/pull/8#discussion_r9527974","pull_request_url":"https://api.github.com/repos/SunriseDigitalStudy/board_matsukawa/pulls/8","_links":{"self":{"href":"https://api.github.com/repos/SunriseDigitalStudy/board_matsukawa/pulls/comments/9527974"},"html":{"href":"https://github.com/SunriseDigitalStudy/board_matsukawa/pull/8#discussion_r9527974"},"pull_request":{"href":"https://api.github.com/repos/SunriseDigitalStudy/board_matsukawa/pulls/8"}}}}