Return to Snippet

Revision: 30680
at August 19, 2010 05:48 by robksawyer


Initial Code
<?php
class User extends AppModel {
	var $name = 'User';
	var $displayField = 'username';
		
	var $validate = array(
						'username' => array(
							'is_unique' => array(
								'rule' => array('isUnique'),
								'message' => 'This username is already registered.'
							),
							'between' => array(
								'rule' => array('between', 4, 30),
								'message' => 'Your username has to be between 4 and 30 characters.'
							)
						),
						'password' => array(
							'compare' => array(
								'rule' => array('compare', array('password_confirm_hash', 'password_confirm')),
								'message' => 'These passwords aren\'t the same.'
							),
							'minlength' => array(
								'rule' => array('wrapper', array(array('minLength', 6), array('password_confirm'))),
								'message' => 'The password must be at least 6 characters and should contain at least one digit.'
							),
							'onedigit' => array(
								'rule' => array('wrapper', array(array('custom', '/[0-9]+/'), array('password_confirm'))),
								'message' => 'The password must be at least 6 characters and should contain at least one digit.'
							)
						),
						'email' => array(
							'email' => array(
								'rule' => array('email'),
								'message' => 'This is not a valid e-mail address.'
							),
							'is_unique' => array(
								'rule' => array('wrapper', array(array('isUnique'), array('email_confirm'))),
								'message' => 'This e-mail address is already registered.'
							),
							'compare' => array(
								'rule' => array('compare', array('email_confirm')),
								'message' => 'These e-mail addresses aren\'t the same.'
							)
						),
						'email_confirm' => array(
							'email' => array(
								'rule' => array('email'),
								'message' => 'This is not a valid e-mail address.',
								'last' => true
							)
						)/*,
						'email_for_credentials' => array(
							'email' => array(
								'rule' => array('email'),
								'message' => 'This is not a valid e-mail address.'
							),
						)*/
	 );
	
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $hasMany = array(
		'Bike' => array(
			'className' => 'Bike',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Part' => array(
			'className' => 'Part',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Company' => array(
			'className' => 'Company',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Vote' => array(
			'className' => 'Vote',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Ownership' => array(
			'className' => 'Ownership',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	var $hasAndBelongsToMany = array(
		'Group' => array('className' => 'Group',
					'joinTable' => 'groups_users',
					'foreignKey' => 'user_id',
					'associationForeignKey' => 'group_id',
					'unique' => true
		)
	);
	
	function wrapper($value, $options = array(), $rule = array()){
		$method = $options[0][0];
		$options[0][0] = $this->data[$this->alias][$options[1][0]];

		if (method_exists($this, $method)) {
			$valid = $this->{$method}($value);
		} else {
			$valid = call_user_func_array(array('Validation', $method), $options[0]);
		}

		if (!$valid) {
			foreach($options[1] as $field) {
				$this->invalidate($field, $rule['message']);
			}
		}

		return $valid;
	}

	function compare($value, $options = array(), $rule = array()) {
		$valid = current($value) == $this->data[$this->alias][$options[0]];
		if (!$valid) {
			$this->invalidate(isset($options[1])? $options[1] : $options[0], $rule['message']);
		}
		return $valid;
	}
}
?>

Initial URL

                                

Initial Description

                                

Initial Title
User Model (cake php)

Initial Tags
php

Initial Language
PHP