学習日記70日目 - Blogapp vol.6 -

スタートアップ研修記はこちらです。

どうも、enomotoです。
今日もBlogapp制作日記です。
本日どうにかブログ投稿画面がほぼ完成しました。
今までタグを投稿するのに別画面で投稿しなければならなかったのが
ブログ記事投稿と同時にできるようになりました。

どうやったか?

処理自体はこうなっています。
ブログ投稿画面(articleモジュール)でブログ内容とタグ内容を記述し送信ボタンを押す
→ブログ内容を保存し、タグ内容をセッションに入れてタグ投稿画面に移動
→タグ内容をセッションから取り出しタグ内容を保存(articletagモジュール)
→タグ内容が入ったセッションを削除してブログ投稿リストに飛ばす
タグ投稿内容はブログ投稿内容を保存すると共にセッションに入れてarticletagモジュールに投げています。


Admin Generatorはそのままではこんなことはやってくれないので自分で実装する必要が…

ということでgenerator.ymlを弄る。

tagパーシャルの呼び出しをする。

generator:
  class:              sfPropelAdminGenerator
  param:
    model_class:      Article
    theme:            default
    list:
      title:          BLOG List
      display:       [id, title, created_at, updated_at]
    edit:
      title:          BLOG Editor
      display:       [title, _tag, body]
tagパーシャルはこんなの。
  <div>Add your own:
      <?php echo input_hidden_tag('article_id', $article->getId()) ?>
  </div>
投稿内容の処理
  protected function updateArticleFromRequest()
  {
    //$usersid = $this->getUser()->getSubscriberId();
    $usersid = 1;
    // まずは投稿者IDをセット
    $this->article->setUserId(1);
    // getRequestParameterする。
    $article = $this->getRequestParameter('article');
    $tag = $this->getRequestParameter('tag');
    // タイトルと内容を保存
    if(isset($article['title'])) {
      $this->article->setTitle($article['title']);
    }
    if(isset($article['body'])) {
      $this->article->setBody($article['body']);
    }
    $this->article->save();
    // 記事IDを呼び出す
    $articleid = $this->article->getId();
    // タグモジュールに送る
    $this->getUser()->setAttribute('userid',$usersid);
    $this->getUser()->setAttribute('articleid',$articleid);
    $this->getUser()->setAttribute('tag',$tag);
    $this->redirect('articletag/set');
  }
タグモジュール
  public function executeSet()
  {
    // タグデータを受け取る
    $userid = $this->getUser()->getAttribute('userid');
    $articleid = $this->getUser()->getAttribute('articleid');
    $tag = $this->getUser()->getAttribute('tag');
    // 普通のタグ挿入
    $this->article_tag = new ArticleTag();
    $this->article_tag->setUserId($userid);
    $this->article_tag->setArticleId($articleid);
    $this->article_tag->setTag($tag);
    $this->article_tag->setNormalizedTag(Tag::normalize($tag));
    // 保存
    $this->article_tag->save();
    // ここから日付タグ
    $this->article_tag = new ArticleTag();
    $this->article_tag->setUserId($userid);
    $this->article_tag->setArticleId($articleid);
    $this->article_tag->setTag(date('Ymd'));
    $this->article_tag->setNormalizedTag(Tag::normalize(date('Ymd')));
    // 保存
    $this->article_tag->save();
    // タグデータを消去
    $this->getUser()->getAttributeHolder()->clear();
    $this->redirect('article/list');
  }