私は$model->created = date("Y-m-d H:m:s");
MySQLに適した形式で現在の日時をかなり頻繁に取得するために使用しており、再利用に適した場所に少しだけ入れたいと思っています。これを行うには多くの方法があることを私は理解していますが、これを達成するための「Yii」の方法は何ですか?
2 に答える
2
ほとんどのモデルが継承するBaseModelがあります。beforeSave()
設定されていない場合は、関数でdateCreatedの設定を行います。また、LocalTimeというコンポーネントを使用しています。http://www.yiiframework.com/wiki/197/local-time-zones-and-locales/を参照してください
class BaseModel extends CActiveRecord {
protected function beforeSave() {
parent::beforeSave();
if($this->hasAttribute('dateCreated') && $this->isNewRecord)
$this->dateCreated = Yii::app()->localtime->getUTCNow();
...
return $this;
}
LocalTimeコンポーネントを使用する場合は、検索時にUTCから現地時間に変換し直すようにしてください。
protected function afterFind() {
parent::afterFind();
if($this->hasAttribute('dateCreated'))
$this->dateCreated = Yii::app()->localtime->fromUTC($this->dateCreated);
...
return $this;
}
于 2013-02-27T09:16:28.597 に答える
1
という名前の yii の組み込み動作を使用できますCTimestampBehavior
。
class YourModel extends CActiveRecord
{
public function behaviors()
{
return array(
'CTimestampBehavior' => array(
'class' => 'zii.behaviors.CTimestampBehavior',
'createAttribute'=>'created',
)
);
}
}
高度にカスタマイズ可能です。updateAttribute
更新時刻の設定、timestampExpression
別の日付形式の指定などを指定することもできます。
于 2013-02-27T11:30:54.080 に答える