phpist

Untitled

Nov 10th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace common\models;
  4.  
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\behaviors\TimestampBehavior;
  8. use yii\db\ActiveRecord;
  9. use yii\db\Expression;
  10.  
  11. /**
  12. * This is the model class for table "todo".
  13. *
  14. * @property int $id
  15. * @property int $user_id
  16. * @property string $title
  17. * @property string $comment
  18. * @property int $status
  19. * @property int $updated_at
  20. * @property int $created_at
  21. */
  22. class Todo extends \yii\db\ActiveRecord
  23. {
  24. /**
  25. * @var int|string
  26. */
  27.  
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return 'todo';
  34. }
  35.  
  36. /**
  37. * @return \yii\db\ActiveQuery
  38. */
  39. /**
  40. * @return \yii\db\ActiveQuery
  41. */
  42. public function getUser()
  43. {
  44. return $this->hasOne(User::class, ['id' => 'user_id']);
  45. }
  46.  
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['user_id', 'status', 'updated_at', 'created_at'], 'integer'],
  54. [['status'], 'default', 'value' => 0],
  55. [['title', 'comment'], 'required'],
  56. [['comment'], 'string'],
  57. [['title'], 'string', 'max' => 50],
  58. ];
  59. }
  60.  
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id' => Yii::t('app', 'ID'),
  68. 'user_id' => Yii::t('app', 'User ID'),
  69. 'title' => Yii::t('app', 'Title'),
  70. 'comment' => Yii::t('app', 'Comment'),
  71. 'status' => Yii::t('app', 'Status'),
  72. 'updated_at' => Yii::t('app', 'Updated At'),
  73. 'created_at' => Yii::t('app', 'Created At'),
  74. ];
  75. }
  76.  
  77. // public function behaviors()
  78. // {
  79. // return [
  80. // [
  81. // 'class' => TimestampBehavior::Todo(),
  82. // 'attributes' => [
  83. // Todo::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
  84. // Todo::EVENT_BEFORE_UPDATE => ['updated_at'],
  85. // ],
  86. // ],
  87. // ];
  88. // }
  89. // public function behaviors()
  90. // {
  91. // return [
  92. // TimestampBehavior::className(),
  93. // ];
  94. // }
  95.  
  96. // public function behaviors()
  97. // {
  98. // return array_merge_recursive(parent::behaviors(), [
  99. // 'TimestampBehavior' => [
  100. // 'class' => TimestampBehavior::class,
  101. // 'createdAtAttribute' => 'created_at',
  102. // 'updatedAtAttribute' => 'updateDate',
  103. // ],
  104. // ]);
  105. // }
  106. // public function behaviors()
  107. // {
  108. // return [
  109. // [
  110. // 'class' => TimestampBehavior::className(),
  111. // 'createdAtAttribute' => 'createDate',
  112. // 'updatedAtAttribute' => 'updateDate',
  113. // 'value' => new Expression('NOW()'),
  114. // ],
  115. // ];
  116. // }
  117. //
  118. // public function behaviors()
  119. // {
  120. // return [
  121. // [
  122. // 'class' => TimestampBehavior::className(),
  123. // ],
  124. // [
  125. // 'class' => TimestampBehavior::className(),
  126. // 'createdAtAttribute' => 'createDate',
  127. // 'updatedAtAttribute' => 'updateDate',
  128. // 'created_at' => function () {
  129. // return date('Y-m-d H:i:s');
  130. // }
  131. // ]
  132. // ];
  133. // }
  134. // public function behaviors()
  135. // {
  136. // return [
  137. // 'timestamp' => [
  138. // 'class' => TimestampBehavior::className(),
  139. // 'attributes' => [
  140. // ActiveRecord::EVENT_BEFORE_INSERT => ['updated_at', 'updated_at'],
  141. // ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
  142. // ],
  143. // 'value' => new Expression('NOW()'),
  144. // ],
  145. // ];
  146. // }
  147.  
  148.  
  149.  
  150. // public function behaviors()
  151. // {
  152. // return [
  153. // [
  154. // 'class' => TimestampBehavior::className(),
  155. // 'createdAtAttribute' => 'createDate',
  156. // 'updatedAtAttribute' => 'updateDate',
  157. // 'value' => new Expression('NOW()'),
  158. // ],
  159. // ];
  160. // }
  161.  
  162. //
  163. // public function behaviors()
  164. // {
  165. // return [
  166. // [
  167. // 'class' => TimestampBehavior::className(),
  168. // ],
  169. // [
  170. // 'class' => TimestampBehavior::className(),
  171. // 'createdAtAttribute' => 'createDate',
  172. // 'updatedAtAttribute' => 'updateDate',
  173. // 'value' => function () {
  174. // return date('Y-m-d H:i:s');
  175. // }
  176. // ]
  177. // ];
  178. // }
  179.  
  180. public function behaviors()
  181. {
  182. return array_merge_recursive(parent::behaviors(), [
  183. 'TimestampBehavior' => [
  184. 'class' => TimestampBehavior::class
  185. ],
  186. ]);
  187. }
  188.  
  189.  
  190.  
  191. public function beforeSave($insert)
  192. {
  193. if ($insert) {
  194. $this->user_id = Yii::$app->user->id;
  195. return true;
  196. }
  197. return false;
  198. }
  199.  
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment