symfonyチュートリアル実践【第4日目途中】

4日目

何気に和訳も見つかりました。
4日目にしてタイトルが「リファクタリング」。もう?と思いましたがチュートリアルには見透かしたように

You may think that it is too early to rewrite code that is only a few days old, but we’ll see how you feel about it at the end of this tutorial.

とあるので黙って進みます。

“A quick look at the action”のところで、

public function executeShow ()
{
    $this->question = QuestionPeer::retrieveByPk($this->getRequestParameter('id'));
    echo $this->getRequestParameter('myparam');
    $this->forward404Unless($this->question);
}

とapps/frontend/modules/question/actions/actions.class.phpを編集すると

http://askeet/frontend_dev.php/question/show/id/1/myparam/hoge

でhogeが出力される。param/value が繋がるので、

http://askeet/frontend_dev.php/question/show/id/1/aaa/hoge/myparam/bbb

とすれば$this->getRequestParameter(‘aaa’);と$this->getRequestParameter(‘myparam’);で値を取得できる。

“Modify the showSuccess.php template”でformat_dateという関数はuse_helper(‘Date’)で呼び出される何かに定義されている模様。use_helper(‘Date’)がないとFatal error: Call to undefined function format_date() in~が出る。ネイティブのdate関数とは違う引数を取る。詳しい説明は別ページにある。

“Add some new test data”の操作でask_answerテーブルにデータが挿入される。frontend/modules/question/templates/showSuccess.phpに書き加えた$question->getAnswers()辺りでそのデータが呼び出される。1つのquestion_idに対して複数のanswerがありうるデータ構造になっている。
自分の環境では(質問をcreateしたりいろいろしたのでserialのidが1より増えたものと思われ)

http://askeet/frontend_dev.php/question/show/id/4

に2つ答えが入っていた。

“Modify the model, part I”ではPHP5のクラスに追加された機能を使って簡単に書けるよ、という例。lib/model/User.phpを以下のように書き換える。

class User extends BaseUser {
    public function __toString()
    {
        return $this->getFirstName(). ' ' . $this->getLastName();
    }
}

するとecho $answer->getUser()でフルネームが出力される仕組み。__toStringについてはPHPのマジックメソッドのマニュアルに書かれている。Neat, isn’t it ?とチュートリアルでは言っていますがわかりやすくなっているかは微妙な気がします。

“Don’t repeat yourself”でリファクタリング。同じコードを2度書くな、というわけで

<div class="interested_mark">
  <?php echo count($question->getInterests()) ?>
</div>

こんなコードが2回出てきそうになったら、

modules/question/templates/_interested_user.php

にコードを避難させて、呼び出すときに

<?php include_partial('interested_user', array('question' => $question)) ?>

としてやる。第2引数で、変数名とその変数に代入する変数を入れる模様。この機能は便利そう。

“Modify the model, part II”のところで突然、$question->getInterests()の部分が重いよね、という話になる。SQL文を一度も書かずにコピペで来たので実感が湧かないが。。さらに恐れを抱かずにテーブル構造を変えよう、と言うがSQLを直接叩かずにやってしまえるこの感覚に慣れないといけない。

config/schema.xmlのask_questionテーブルの最後の方に

<column name="interested_users" type="integer" default="0" />

を書き加えていくつかコマンド実行。

$ mysql -u youruser -p askeet < data/sql/schema.sql

の箇所で前回も出た構文がおかしいよエラー。根本的な解決をしていなかったのでまた手動で書き直す羽目に。

save()をbeginとcommitで囲んで

http://askeet/frontend_dev.php/question/show/id/4

にアクセスしたらOops! テーブルをリビルドした際にidがリセットされたらしい。4ではなく1にアクセス。

Fatal error: Call to undefined method Question::getInterestedUsers()

と出た。grepで調べるとgetInterestedUsersはlib/model/om/BaseAskQuestion.php内でちゃんと定義されている。でも$questionはget_class_methodsで見るとlib/model/om/BaseQuestion.phpのクラスっぽい。getInterestsをgrepするとそちらが出てくる。

ひとしきり悩んでもわからず、、明日へ持ち越し。

This entry was posted in いじる. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Page optimized by WP Minify WordPress Plugin