Created
May 11, 2011 21:33
-
-
Save nanodocumet/967416 to your computer and use it in GitHub Desktop.
Model Testuser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
class Controller_Test extends Controller { | |
public function action_index() | |
{ | |
$user = new Model_Testuser; | |
$user_fields = array( | |
'username' => 'test', | |
'email' => 'email@email.com', | |
'password' => 'password', | |
); | |
// Set dummy fields | |
$user->set_fields($user_fields); | |
// Save user | |
$user->save(); | |
// Update the number of logins | |
$user->logins++; | |
// Set the last login date | |
$user->last_login = time(); | |
// Save the user | |
$user->save(); | |
} | |
} // End Auth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Testuser Model | |
* | |
*/ | |
class Model_Testuser extends AutoModeler_ORM { | |
protected $_table_name = 'testusers'; | |
protected $_data = array('id' => '', | |
'username' => '', | |
'email' => '', | |
'password' => '', | |
'last_login' => '', | |
'logins' => ''); | |
protected $_rules = array( | |
'username' => array( | |
array('not_empty'), | |
array('min_length', array(':value', 4)), | |
array('max_length', array(':value', 32)), | |
array('regex', array(':value', '/^[-\pL\pN_.]++$/uD')), | |
array(array('Model_Testuser', 'username_available'), array(':validation', ':field')), | |
), | |
'email' => array( | |
array('not_empty'), | |
array('min_length', array(':value', 4)), | |
array('max_length', array(':value', 127)), | |
array('email'), | |
array(array('Model_Testuser', 'email_available'), array(':validation', ':field')), | |
), | |
'password' => array( | |
array('not_empty'), | |
), | |
); | |
/** | |
* Does the reverse of unique_key_exists() by triggering error if username exists. | |
* Validation callback. | |
* | |
* @param Validation Validation object | |
* @param string Field name | |
* @return void | |
*/ | |
public static function username_available(Validation $validation, $field) | |
{ | |
if (self::unique_key_exists($validation[$field], 'username')) | |
{ | |
$validation->error($field, 'username_available', array($validation[$field])); | |
} | |
} | |
/** | |
* Does the reverse of unique_key_exists() by triggering error if email exists. | |
* Validation callback. | |
* | |
* @param Validation Validation object | |
* @param string Field name | |
* @return void | |
*/ | |
public static function email_available(Validation $validation, $field) | |
{ | |
if (self::unique_key_exists($validation[$field], 'email')) | |
{ | |
$validation->error($field, 'email_available', array($validation[$field])); | |
} | |
} | |
/** | |
* Tests if a unique key value exists in the database. | |
* | |
* @param mixed the value to test | |
* @param string field name | |
* @return boolean | |
*/ | |
public static function unique_key_exists($value, $field = NULL) | |
{ | |
if ($field === NULL) | |
{ | |
// Automatically determine field by looking at the value | |
$field = self::unique_key($value); | |
} | |
$user = new Model_Testuser; | |
return (bool) db::select(array('COUNT("*")', 'total_count')) | |
->from($user->_table_name) | |
->where($field, '=', $value) | |
->execute($user->_db) | |
->get('total_count'); | |
} | |
/** | |
* Allows a model use both email and username as unique identifiers for login | |
* | |
* @param string unique value | |
* @return string field name | |
*/ | |
public static function unique_key($value) | |
{ | |
return Valid::email($value) ? 'email' : 'username'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment