This tutorial will guide you through setting up and customizing timestamps (created_at and updated_at) in your Yii models. Yii provides built-in functionality to automatically manage these timestamps, simplifying database interactions and ensuring data integrity. We'll cover various methods and customization options.
Yii offers a straightforward way to automatically generate created_at
and updated_at
timestamps using the behaviors
property within your model. This leverages the TimestampBehavior
which handles the automatic population of these attributes.
To implement this, add the TimestampBehavior
to your model's behaviors()
method:
<?php namespace app\models; use yii\db\ActiveRecord; use yii\behaviors\TimestampBehavior; class MyModel extends ActiveRecord { public static function tableName() { return 'my_table'; } public function behaviors() { return [ TimestampBehavior::class, ]; } // ... other model code ... }
This simple addition automatically populates created_at
upon record creation and updated_at
on every update. The behavior assumes your table has columns named created_at
and updated_at
of a suitable timestamp data type (e.g., TIMESTAMP
, DATETIME
). If your column names differ, you can specify them using the attributes
property within the TimestampBehavior
configuration:
public function behaviors() { return [ [ 'class' => TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], //Optional: Customize value attribute (see next section for details) //'value' => new Expression('NOW()'), ], ]; }
This allows for fine-grained control over which attributes are updated during insertion and update events.
Besides the TimestampBehavior
, there are other ways to handle timestamps, though the behavior is generally the preferred and most efficient method. Alternative approaches include:
beforeSave()
method. This offers more control but requires more code and increases the risk of errors if not handled carefully.<?php namespace app\models; use yii\db\ActiveRecord; use yii\behaviors\TimestampBehavior; class MyModel extends ActiveRecord { public static function tableName() { return 'my_table'; } public function behaviors() { return [ TimestampBehavior::class, ]; } // ... other model code ... }
TimestampBehavior
offers, you can create your own behavior extending TimestampBehavior
or creating a completely new one. This provides the greatest flexibility but necessitates a deeper understanding of Yii's behavior mechanism.While the TimestampBehavior
doesn't directly allow customizing the format of the timestamp (it uses the database's default handling), you can control the value assigned to the timestamp attributes. You can use a yiidbExpression
to achieve custom timestamp generation, for instance, to use a specific function from your database system.
For example, to always use the database's NOW()
function (regardless of your PHP timezone):
public function behaviors() { return [ [ 'class' => TimestampBehavior::class, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'], ], //Optional: Customize value attribute (see next section for details) //'value' => new Expression('NOW()'), ], ]; }
Remember to adjust your database column type and potentially your database settings to handle the specific timestamp format generated by your chosen expression. Formatting for display purposes should be handled within your view using PHP's date functions or Yii's date formatting helpers. For instance, using Yii::$app->formatter->asDate($model->created_at)
in your view will format the timestamp according to your application's settings.
The above is the detailed content of How to set the yii frame timestamp tutorial. For more information, please follow other related articles on the PHP Chinese website!