ORM 构造和add 方法
利用ORM存储一行信息:
$user = ORM::factory('user'); $user->username = 'admin'; $user->email = 'admingmail.com'; $user->password = 'good_pass'; $user->save();
$user = ORM::factory('user'); $user->username = 'admin'; $user->email = 'admingmail.com'; $user->password = 'good_pass'; $user->save(); // dont forget to add roles. 'login' role needs for successful login $user->add('role', ORM::factory('role', array('name' => 'admin'))); $user->add('role', ORM::factory('role', array('name' => 'login')));
ORM::factor('role',array('name'=>'admin'))
这段意思是 找到 字段名 name值为 admin 返回他的主键。
如何使用AUTH控制登录:
配置文件 auth.php:
return array( 'driver' => 'File', 'hash_method' => 'sha256', 'hash_key' => "xxxx", 'lifetime' => 1209600, 'session_type' => Session::$default, 'session_key' => 'auth_user', // Username/password combinations for the Auth File driver 'users' => array( 'admin' => '86aa66fdd3385a80a2c4f0c658f0a85cf0297934ce67b7e0ca50d35b3b000962', ), );
$user = ORM::factory('user'); $user->username = 'admin'; $user->email = 'admingmail.com'; $user->password = 'good_pass'; $user->save(); // dont forget to add roles. 'login' role needs for successful login $user->add('role', ORM::factory('role', array( 'name' => 'admin' ))); $user->add('role', ORM::factory('role', array( 'name' => 'login' ))); $auth = Auth::instance(); if ($auth->login('admin', 'good_pass', true)) { echo 'hello, ' . $auth->get_user(); } else { echo 'login failed!'; }
这段代码用户名和密码是保存文件中,实际可以写在配置文件中。
auth可以实现的是保存SESSION。
实现自动登录,无需输入用户名密码。