User Model (cake php)


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. class User extends AppModel {
  3. var $name = 'User';
  4. var $displayField = 'username';
  5.  
  6. var $validate = array(
  7. 'username' => array(
  8. 'is_unique' => array(
  9. 'rule' => array('isUnique'),
  10. 'message' => 'This username is already registered.'
  11. ),
  12. 'between' => array(
  13. 'rule' => array('between', 4, 30),
  14. 'message' => 'Your username has to be between 4 and 30 characters.'
  15. )
  16. ),
  17. 'password' => array(
  18. 'compare' => array(
  19. 'rule' => array('compare', array('password_confirm_hash', 'password_confirm')),
  20. 'message' => 'These passwords aren\'t the same.'
  21. ),
  22. 'minlength' => array(
  23. 'rule' => array('wrapper', array(array('minLength', 6), array('password_confirm'))),
  24. 'message' => 'The password must be at least 6 characters and should contain at least one digit.'
  25. ),
  26. 'onedigit' => array(
  27. 'rule' => array('wrapper', array(array('custom', '/[0-9]+/'), array('password_confirm'))),
  28. 'message' => 'The password must be at least 6 characters and should contain at least one digit.'
  29. )
  30. ),
  31. 'email' => array(
  32. 'email' => array(
  33. 'rule' => array('email'),
  34. 'message' => 'This is not a valid e-mail address.'
  35. ),
  36. 'is_unique' => array(
  37. 'rule' => array('wrapper', array(array('isUnique'), array('email_confirm'))),
  38. 'message' => 'This e-mail address is already registered.'
  39. ),
  40. 'compare' => array(
  41. 'rule' => array('compare', array('email_confirm')),
  42. 'message' => 'These e-mail addresses aren\'t the same.'
  43. )
  44. ),
  45. 'email_confirm' => array(
  46. 'email' => array(
  47. 'rule' => array('email'),
  48. 'message' => 'This is not a valid e-mail address.',
  49. 'last' => true
  50. )
  51. )/*,
  52. 'email_for_credentials' => array(
  53. 'email' => array(
  54. 'rule' => array('email'),
  55. 'message' => 'This is not a valid e-mail address.'
  56. ),
  57. )*/
  58. );
  59.  
  60. //The Associations below have been created with all possible keys, those that are not needed can be removed
  61. var $hasMany = array(
  62. 'Bike' => array(
  63. 'className' => 'Bike',
  64. 'foreignKey' => 'user_id',
  65. 'dependent' => false,
  66. 'conditions' => '',
  67. 'fields' => '',
  68. 'order' => '',
  69. 'limit' => '',
  70. 'offset' => '',
  71. 'exclusive' => '',
  72. 'finderQuery' => '',
  73. 'counterQuery' => ''
  74. ),
  75. 'Part' => array(
  76. 'className' => 'Part',
  77. 'foreignKey' => 'user_id',
  78. 'dependent' => false,
  79. 'conditions' => '',
  80. 'fields' => '',
  81. 'order' => '',
  82. 'limit' => '',
  83. 'offset' => '',
  84. 'exclusive' => '',
  85. 'finderQuery' => '',
  86. 'counterQuery' => ''
  87. ),
  88. 'Company' => array(
  89. 'className' => 'Company',
  90. 'foreignKey' => 'user_id',
  91. 'dependent' => false,
  92. 'conditions' => '',
  93. 'fields' => '',
  94. 'order' => '',
  95. 'limit' => '',
  96. 'offset' => '',
  97. 'exclusive' => '',
  98. 'finderQuery' => '',
  99. 'counterQuery' => ''
  100. ),
  101. 'Vote' => array(
  102. 'className' => 'Vote',
  103. 'foreignKey' => 'user_id',
  104. 'dependent' => false,
  105. 'conditions' => '',
  106. 'fields' => '',
  107. 'order' => '',
  108. 'limit' => '',
  109. 'offset' => '',
  110. 'exclusive' => '',
  111. 'finderQuery' => '',
  112. 'counterQuery' => ''
  113. ),
  114. 'Ownership' => array(
  115. 'className' => 'Ownership',
  116. 'foreignKey' => 'user_id',
  117. 'dependent' => false,
  118. 'conditions' => '',
  119. 'fields' => '',
  120. 'order' => '',
  121. 'limit' => '',
  122. 'offset' => '',
  123. 'exclusive' => '',
  124. 'finderQuery' => '',
  125. 'counterQuery' => ''
  126. )
  127. );
  128.  
  129. var $hasAndBelongsToMany = array(
  130. 'Group' => array('className' => 'Group',
  131. 'joinTable' => 'groups_users',
  132. 'foreignKey' => 'user_id',
  133. 'associationForeignKey' => 'group_id',
  134. 'unique' => true
  135. )
  136. );
  137.  
  138. function wrapper($value, $options = array(), $rule = array()){
  139. $method = $options[0][0];
  140. $options[0][0] = $this->data[$this->alias][$options[1][0]];
  141.  
  142. if (method_exists($this, $method)) {
  143. $valid = $this->{$method}($value);
  144. } else {
  145. $valid = call_user_func_array(array('Validation', $method), $options[0]);
  146. }
  147.  
  148. if (!$valid) {
  149. foreach($options[1] as $field) {
  150. $this->invalidate($field, $rule['message']);
  151. }
  152. }
  153.  
  154. return $valid;
  155. }
  156.  
  157. function compare($value, $options = array(), $rule = array()) {
  158. $valid = current($value) == $this->data[$this->alias][$options[0]];
  159. if (!$valid) {
  160. $this->invalidate(isset($options[1])? $options[1] : $options[0], $rule['message']);
  161. }
  162. return $valid;
  163. }
  164. }
  165. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.