Footprint.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace common\models;
  3. use common\behaviors\SoftDeleteBehavior;
  4. use common\enums\StatusEnum;
  5. use yii\behaviors\BlameableBehavior;
  6. use yii\behaviors\TimestampBehavior;
  7. /**
  8. * This is the model class for table "{{%vote}}".
  9. *
  10. * @property int $id
  11. * @property string $entity
  12. * @property string $entity_id
  13. * @property int $user_id
  14. * @property int $created_at
  15. * @property int $updated_at
  16. * @property int $status
  17. */
  18. class Footprint extends \yii\db\ActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%footprint}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['entity', 'entity_id'], 'required'],
  34. [['user_id', 'entity_id'], 'integer'],
  35. [['entity'], 'string', 'max' => 255],
  36. ['status', 'default', 'value' => StatusEnum::STATUS_ON],
  37. [['user_id'], 'safe']
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'entity' => 'entity',
  48. 'user_id' => 'User ID',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. public function behaviors()
  54. {
  55. return [
  56. TimestampBehavior::className(),
  57. [
  58. 'class' => SoftDeleteBehavior::className(),
  59. 'softDeleteAttributeValues' => [
  60. 'updated_at' => time(),
  61. 'status' => StatusEnum::STATUS_OFF,
  62. ],
  63. 'invokeDeleteEvents' => false // 不触发删除相关事件
  64. ],
  65. [
  66. 'class' => BlameableBehavior::className(),
  67. 'createdByAttribute' => 'user_id',
  68. 'updatedByAttribute' => false,
  69. ],
  70. ];
  71. }
  72. /**
  73. * @return query\FootprintQuery|\yii\db\ActiveQuery
  74. * @author nodelog
  75. */
  76. public static function find()
  77. {
  78. return new \common\models\query\FootprintQuery(get_called_class());
  79. }
  80. /**
  81. * 添加访问记录
  82. * @author nodelog
  83. */
  84. public static function add($entity, $entity_id)
  85. {
  86. if (!\Yii::$app->user->isGuest) {
  87. $model = new Footprint();
  88. $model->entity = $entity;
  89. $model->entity_id = $entity_id;
  90. return $model->save();
  91. }
  92. return false;
  93. }
  94. }