LoginSignup
19
20

More than 5 years have passed since last update.

FuelPHPのORMをフォーマット出力するパッケージが便利すぎるよ

Posted at

概要

このパッケージでは、ORMからの出力を以下のように簡単に記述できるようになります。

app/views/*.php
<?php echo ($person->age)? sprintf('%d歳',$person->age):'';?>
↓↓↓
<?php echo $person->formatted_age; ?>

DBに格納している値に単位をつけて出力したいケースは多く、また、値が0やNULLだった場合は単位なしで空文字を出力したいといったケースも多いです。
通常はいちいちif文や三項演算子を使ってViewを書いていくと思いますが、非常に冗長になるため、ORMに機能を追加してViewの記述を簡略化してしまおうというものです。
もちろん、その他のメリットも多々あります。

パッケージ

Fuel-Package-TraitOrmFormat(ver.2.0)
※ブランチ「2.0」が最新版です。(masterは古い)
※PHP5.4からの機能(Trait)を使っていますので、PHP5.4以上の環境が必要です。

インストール

composer.json

    "require": {
        "php": ">=5.4",
        "trait-orm-format": "1.*"
    },
    "repositories": [ 
        {
            "type":"package",
            "package":{
                "name":"trait-orm-format",
                "type":"fuel-package",
                "version":"1.7",
                "source":{
                    "type":"git",
                    "url":"https://github.com/goosys/Fuel-Package-TraitOrmFormat.git",
                    "reference":"2.0"
                }
            }
        }
    ],
fuel/app/config/config.php
always_load => array(
    packages => array(
        'trait-orm-format'
    ),
    language => array(
        'selector',
        'trait-orm-format'
    )
fuel/app/classes/model/*.php
class Model_Person extends \Orm\Model
{
    use Trait_Orm_Format; //この行を追加

機能

今までも便利でしたが、不具合修正と多少の機能追加をした2.0が更に便利になっています。
まずは基本的な使い方の説明です。

フォーマットを定義

fuel/app/lang/ja/selector.php
    'gender' => array(
        '1' => '男性',
        '2' => '女性'
    ),
    'country' => array(
        'jp' => '日本',
        'us' => 'アメリカ',
    )
fuel/app/lang/ja/trait-orm-format.php
    'common' => array(
        'people' => '%d人', //文字列の場合はsprintfにかけて出力
        'age'    => '%d歳',
        'jpy'    => function($val){ return number_format($val).'円'; }, //関数の場合はreturnされた文字列を出力
        'timetodate' => function($val){ return (!empty($val))? date('Y/m/d',$val): ''; }, //created_atなどのタイムスタンプを日付にフォーマットする場合に便利
    ),
    'model' => array(
        'person' => array(
            'age'         => 'common.age',          //共通フォーマットから選択
            'num_friends' => '%d人',                //フォーマットを直接記述
            'gender'      => 'selector.gender',     //選択肢から選択
            'full_address'=> 'related.address.full_address' //$person->address->full_address
            'local'       => 'related.address.formatted_country' //リレーションしたモデルからも再帰的にフォーマット可能
        ),
        'address' => array(
            //'full_address' => '%s',             //そのまま出力してよいものは省略可
            'country'      => 'selector.country', //選択肢から選択
        )
    )

Viewで使用

Viewではこの様に統一的に記述できます。(コメント内は出力結果の例)

fuel/app/views/*.php
<?php echo $person->formatted_age; /* 12歳 */ ?>
<?php echo $person->formatted_num_friends; /* 100人 */ ?>
<?php echo $person->formatted_gender; /* 男性 */ ?>
<?php echo $person->formatted_local; /* 日本 */ ?>

2.0からの新機能

2.0から、以下2機能が追加されました。

リレーション先のモデルも再帰的にフォーマットする機能

fuel/app/views/*.php
<?php echo $person->formatted_full_address; /* 東京都新宿区1-1-1-1 */ ?>

Model_Person has_one Model_Addressとリレーションされていて、$person->address->full_address='東京都新宿区1-1-1-1'となっていた場合、empty($person->address)で実際にデータが入っているかをチェックした上で、Model_Addressfull_addressに定義されたフォーマットで出力を行います。

同名の関数を実行する機能

モデルにformatted_で始まる関数が定義されていた場合、

fuel/app/classes/model/person.php
public function formatted_full_name(){
    return $this->family_name . ' '. $this->first_name;
}

このようにアクセスすることができます。

fuel/app/views/*.php
<?php echo $person->formatted_full_name; /* 田中 太郎 */ ?>

もちろん、$person->formatted_full_name()でも同様の結果となりますが、書き方を統一でき、また、リレーション先のフォーマットとしても使用することが出来るので便利です。

19
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
20