数据库行为

模型行为用于实现通用功能。 与Traits(特征)不同,这些可以直接实现 in a class or by extending the class. You can read more about behaviors here. 在class类中或通过扩展class类。 您可以阅读更多有关行为此处的内容。

可清除的

创建或更新模型时,清除的属性不会保存到数据库中。 清除 模型中的属性,实现October.Rain.Database.Behaviors.Purgeable行为并声明 一个$purgeable属性,带有一个包含要清除的属性的数组。

  1. class User extends Model
  2. {
  3. public $implement = [
  4. 'October.Rain.Database.Behaviors.Purgeable'
  5. ];
  6. /**
  7. * @var array List of attributes to purge.
  8. */
  9. public $purgeable = [];
  10. }

您还可以在类中动态实现此行为。

  1. /**
  2. * 扩展RainLab.User用户模型以实现可清除行为。
  3. */
  4. RainLab\User\Models\User::extend(function($model) {
  5. // Implement the purgeable behavior dynamically
  6. $model->implement[] = 'October.Rain.Database.Behaviors.Purgeable';
  7. // Declare the purgeable property dynamically for the purgeable behavior to use
  8. $model->addDynamicProperty('purgeable', []);
  9. });

保存模型时,在模型事件之前将清除定义的属性 被触发,包括验证。 使用getOriginalPurgeValue查找已清除的值。

  1. return $user->getOriginalPurgeValue($propertyName);