2013-07-01 6 views
5

現在、データベースの行にユーザーのタイムゾーンを保存しており、日付を印刷するたびにユーザーのタイムゾーンに変換しています。どのようにDRYのやり方でこれを行うことができますか?Laravel 4でユーザーのタイムゾーンを変更する最も良い方法は何ですか?

EloquentがCarbon DateTimeオブジェクトを返す場所をオーバーライドする必要があります。もしそうなら、私は以下のようにこれを特性に入れなければならないので、一度だけ書く必要があります。

<?php 

use Carbon\Carbon; 
use Illuminate\Database\Eloquent; 

trait ConvertTimeZone { 
/** 
* Return a timestamp as DateTime object. 
* 
* @param mixed $value 
* @return DateTime 
*/ 
protected function asDateTime($value) 
{ 
    // If this value is an integer, we will assume it is a UNIX timestamp's value 
    // and format a Carbon object from this timestamp. This allows flexibility 
    // when defining your date fields as they might be UNIX timestamps here. 
    if (is_numeric($value)) 
    { 
     return Carbon::createFromTimestamp($value); 
    } 

    // If the value is in simply year, month, day format, we will instantiate the 
    // Carbon instances from that fomrat. Again, this provides for simple date 
    // fields on the database, while still supporting Carbonized conversion. 
    elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) 
    { 
     return Carbon::createFromFormat('Y-m-d', $value); 
    } 

    // Finally, we will just assume this date is in the format used by default on 
    // the database connection and use that format to create the Carbon object 
    // that is returned back out to the developers after we convert it here. 
    elseif (! $value instanceof DateTime) 
    { 
     $format = $this->getDateFormat(); 

     $timezone = \Auth::user()->timezone; 

     return Carbon::createFromFormat($format, $value, $timezone); 
    } 

    return Carbon::instance($value); 
} 

}

答えて

6

私はからそのような機能を必要とするモデルを拡張したい、そこからEloquentを延びる、BaseModelクラスを作成することになります。ユーザーがログインしているかどうかを確認して、タイムゾーンを取得できるようにしてください。例:BaseModelを拡張する形質を使用するよりも優れている理由を

モデル/ BaseModel.php

class BaseModel extends Illuminate\Database\Eloquent\Model { 

    protected function asDateTime($value) { 

     // If Carbon receives null, it knows to use the default timezone 
     $tz = null; 

     // If the user is logged in, get it's timezone 
     if (Auth::check()) { 
      $tz = Auth::user()->timezone; 
     } 

     // If this value is an integer, we will assume it is a UNIX timestamp's value 
     // and format a Carbon object from this timestamp. This allows flexibility 
     // when defining your date fields as they might be UNIX timestamps here. 
     if (is_numeric($value)) { 
      return Carbon::createFromTimestamp($value, $tz); 
     } 

     // If the value is in simply year, month, day format, we will instantiate the 
     // Carbon instances from that fomrat. Again, this provides for simple date 
     // fields on the database, while still supporting Carbonized conversion. 
     elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value)) { 
      return Carbon::createFromFormat('Y-m-d', $value, $tz); 
     } 

     // Finally, we will just assume this date is in the format used by default on 
     // the database connection and use that format to create the Carbon object 
     // that is returned back out to the developers after we convert it here. 
     elseif (! $value instanceof DateTime) { 
      $format = $this->getDateFormat(); 

      return Carbon::createFromFormat($format, $value, $tz); 
     } 

     return Carbon::instance($value); 
    } 
} 

モデル/ User.php

class User extends BaseModel { 
    // ... 
} 
+0

あなたは私を教えていただけますか?ありがとう –

+0

それは良いことではない、形質の事実は実際にはかなりいいですし、まったく同じように動作します。これは、私がLaravelと仕事を始めてから私がやってきたことです。私はそれを分かち合うことができると考えました。これの唯一の利点は、もちろん、PHP 5.4.0を必要としないことですが、それはおそらく大きな懸念事項ではありません。 – rmobis

+0

ミューテータを使う方が良いかもしれません。 –

関連する問題