tegarkurniawan

mymodel

Jul 9th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This is the model class for table "user".
  5. *
  6. * The followings are the available columns in table 'user':
  7. * @property string $id
  8. * @property string $type
  9. * @property string $company_id
  10. * @property string $password
  11. * @property string $email
  12. * @property string $name
  13. * @property string $status
  14. */
  15. class User extends CActiveRecord
  16. {
  17. public $old_password;
  18. public $new_password;
  19. public $repeat_password;
  20. /**
  21. * Returns the static model of the specified AR class.
  22. * @param string $className active record class name.
  23. * @return User the static model class
  24. */
  25. public static function model($className=__CLASS__)
  26. {
  27. return parent::model($className);
  28. }
  29.  
  30. /**
  31. * @return string the associated database table name
  32. */
  33. public function tableName()
  34. {
  35. return 'user';
  36. }
  37.  
  38. /**
  39. * @return array validation rules for model attributes.
  40. */
  41. public function rules()
  42. {
  43. // NOTE: you should only define rules for those attributes that
  44. // will receive user inputs.
  45. return array(
  46. array('email, password, repeat_password, phone', 'required', 'on' => 'register'),
  47. array('email','unique'),
  48. array('email','email'),
  49. array('email','unique', 'on'=>'createPwd'),
  50. array('type, status', 'length', 'max'=>10),
  51. array('password', 'length', 'max'=>50),
  52. array('phone', 'length', 'max'=>50),
  53. array('email, auth_key, access_token, ', 'length', 'max'=>255),
  54. array('old_password, new_password, repeat_password', 'required', 'on' => 'changePwd'),
  55. array('old_password', 'findPasswords', 'on' => 'changePwd'),
  56. array('repeat_password', 'compare', 'compareAttribute'=>'new_password', 'on'=>'changePwd'),
  57. array('password', 'compare', 'compareAttribute'=>'repeat_password', 'on'=>'createPwd'),
  58. // The following rule is used by search().
  59. // Please remove those attributes that should not be searched.
  60. array('id, type, password, client_id, phone, email, auth_key, access_token, hash, status', 'safe', 'on'=>'search'),
  61. );
  62. }
  63.  
  64. /**
  65. * @return array relational rules.
  66. */
  67. public function relations()
  68. {
  69. // NOTE: you may need to adjust the relation name and the related
  70. // class name for the relations automatically generated below.
  71. return array(
  72. // 'company' => array(self::BELONGS_TO, 'Company', 'company_id'),
  73. // 'userdetail' => array(self::BELONGS_TO, 'UserDetail', 'user_id'),
  74. 'userdetail' => array(self::BELONGS_TO, 'UserDetail', 'user_id'),
  75. 'client' => array(self::BELONGS_TO, 'Client', 'client_id'),
  76. );
  77. }
  78.  
  79. /**
  80. * @return array customized attribute labels (name=>label)
  81. */
  82. public function attributeLabels()
  83. {
  84. return array(
  85. 'id' => 'ID',
  86. 'type' => 'Type',
  87. 'client_id' => 'client_id',
  88. 'password' => 'Password',
  89. 'new_password' => 'New Password',
  90. 'old_password' => 'Old Password',
  91. 'email' => 'Email',
  92. 'phone' => 'Phone',
  93. 'auth_key' => 'Auth Key',
  94. 'access_token' => 'Auth Token',
  95. 'hash' => 'Hash',
  96. 'status' => 'Status',
  97.  
  98. );
  99. }
  100.  
  101. /**
  102. * Retrieves a list of models based on the current search/filter conditions.
  103. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  104. */
  105. public function search()
  106. {
  107. // Warning: Please modify the following code to remove attributes that
  108. // should not be searched.
  109.  
  110. $criteria=new CDbCriteria;
  111.  
  112. $criteria->compare('id',$this->id,false);
  113. $criteria->compare('type',$this->type,false);
  114. $criteria->compare('client_id',$this->client_id,false);
  115. $criteria->compare('password',$this->password,false);
  116. $criteria->compare('email',$this->email,false);
  117. $criteria->compare('phone',$this->email,true);
  118. $criteria->compare('auth_key',$this->auth_key,false);
  119. $criteria->compare('access_token',$this->access_token,false);
  120. $criteria->compare('hash',$this->hash,false);
  121. $criteria->compare('status',$this->status,false);
  122.  
  123. return new CActiveDataProvider($this, array(
  124. 'criteria'=>$criteria,
  125. ));
  126. }
  127.  
  128. public function findPasswords($attribute, $params)
  129. {
  130. $user = User::model()->findByPk($this->id);
  131. if ($user->password != sha1($this->old_password)){
  132. $this->addError($attribute, 'Old password is incorrect.');
  133. }
  134. }
  135. public function beforeSave(){
  136. if($this->isNewRecord)
  137. {
  138. $this->hash = sha1($this->id . date('Y') . $this->email . date('m-d H:i:s'));
  139. }
  140.  
  141. return parent::beforeSave();
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment