pmtpenza

Untitled

Sep 5th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <?php
  2. namespace api\models;
  3. use common\models\User;
  4. use backend\models\AuthAssignment;
  5. use Yii;
  6. use yii\base\Exception;
  7. use yii\base\Model;
  8. use yii\helpers\ArrayHelper;
  9. /**
  10. * Create user form
  11. */
  12. class UserForm extends Model
  13. {
  14. public $username;
  15. public $password;
  16. public $status;
  17. public $phone;
  18. public $created_at;
  19. public $updated_at;
  20. public $id;
  21. public $fio;
  22. public $gender;
  23. public $birthdate;
  24. public $description;
  25. public $avatar;
  26.  
  27. private $model;
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['phone'], 'required'],
  35. [['status'], 'integer'],
  36. [['created_at', 'updated_at'], 'safe'],
  37. [['username'], 'string', 'max' => 255],
  38. [['phone'], 'string', 'max' => 50],
  39. ['phone', 'unique', 'targetClass' => User::class, 'filter' => function ($query) {
  40. if (!$this->getModel()->isNewRecord) {
  41. $query->andWhere(['not', ['id' => $this->getModel()->id]]);
  42. }
  43. }],
  44. ['password', 'required', 'on' => 'create'],
  45. ['password', 'string', 'min' => 6],
  46. ];
  47. }
  48. /**
  49. * @return User
  50. */
  51. public function getModel()
  52. {
  53. if (!$this->model) {
  54. $this->model = new User();
  55. }
  56. return $this->model;
  57. }
  58. /**
  59. * @param User $model
  60. * @return mixed
  61. */
  62. public function setModel($model)
  63. {
  64. $this->username = $model->username;
  65. $this->status = $model->status;
  66. $this->phone = $model->phone;
  67. $this->phone = $model->fio;
  68. $this->phone = $model->gender;
  69. $this->phone = $model->birthdate;
  70. $this->phone = $model->description;
  71. $this->phone = $model->avatar;
  72. $this->model = $model;
  73.  
  74. return $this->model;
  75. }
  76. /**
  77. * @inheritdoc
  78. */
  79. public function attributeLabels()
  80. {
  81. return [
  82. 'username' => 'Имя',
  83. 'password' => 'Пароль',
  84. 'phone' => 'Телефон',
  85.  
  86. ];
  87. }
  88. /**
  89. * Signs user up.
  90. * @return User|null the saved model or null if saving fails
  91. * @throws Exception
  92. */
  93. public function save()
  94. {
  95. if ($this->validate()) {
  96. $model = $this->getModel();
  97. $isNewRecord = $model->getIsNewRecord();
  98. $model->username = $this->username;
  99. $model->phone = $this->phone;
  100. $model->fio = $this->fio;
  101. $model->gender = $this->gender;
  102. $model->birthdate = $this->birthdate;
  103. $model->description = $this->description;
  104. $model->avatar = $this->avatar;
  105.  
  106. $model->status = $this->status;
  107. if ($this->password) {
  108. $model->setPassword($this->password);
  109. }
  110. if (!$model->save()) {
  111. throw new Exception('Model not saved');
  112. }
  113. }
  114. return $model;
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment