Auth::instance()->create_user()to create users, but I have another table which is related to the simpleusers table. So how would you combine it so when user is logged in, you can insert other properties in the second table? I wasn't sure if another model was needed for the simpleusers table (but as documented, its says you need to have both belongs_to and has_many for it to work) So I have teh following to models: user:
<?php class Model_User extends Orm\Model { protected static $_table_name = 'simpleusers'; protected static $_properties = array('id', 'username', 'password', 'email', 'profile_fields', 'group', 'last_login', 'login_hash'); $_has_many = array( 'api' => array( 'key_from' => 'id', 'model_to' => 'Model_Api', 'key_to' => 'user_id', 'cascade_save' => true, 'cascade_delete' => false, )); }
<?php class Model_Api extends Orm\Model { protected static $_table_name = 'simpleusers_api'; protected static $_properties = array('id', 'api_id', 'api_key', 'user_id'); $_belongs_to = array( 'user' => array( 'key_from' => 'user_id', 'model_to' => 'Model_User', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, )); )
$api = Model_Api::find(5); // fetch api with pk = 5 $user->apis[] = $api; $user->save(); // after saving the api relation to the user has been saved and reindexed to have array key = PK (thus 5) // if we want to delete it again: unset($user->apis[$api->id]); // or just 5 instead of $api->id $user->save(); // for singular relations (HasOne, BelongsTo) its similar: $api->user = $user; // relate api to user $api->save(); $api->user = null; // unrelate the user $api->save();
if (Auth::instance()->create_user($val->validated('username'), $val->validated('password'), $val->validated('email'), 1)) { Auth::instance()->login($val->validated('username'), $val->validated('password')); $api = new Model_api(array( 'api_id' => $val->validated('api_id'),, 'api_key' => $val->validated('api_key'),, 'user_id' => $this->user_id, )); $api->save(); Session::set_flash('success', 'Thanks for registering!'); Response::redirect('/'); }
if (Auth::instance()->create_user($val->validated('username'), $val->validated('password'), $val->validated('email'), 1)) { Auth::instance()->login($val->validated('username'), $val->validated('password')); $api = new Model_api(array( 'api_id' => $val->validated('api_id'), 'api_key' => Model_Api::encryptApiKey($val->validated('api_key')), 'user_id' => $this->user_id, )); $api->save(); Model_Api::saveCharacters($api_id, $api_key); Session::set_flash('success', 'Thanks for registering!'); Response::redirect('/'); }
'user_id' => $this->user_id,
// assign the user ID variable while creating the user: if ($user_id = Auth::instance()->create_user($val->validated('username'), $val->validated('password'), $val->validated('email'), 1)) { Auth::instance()->login($val->validated('username'), $val->validated('password')); $api = new Model_api(array( 'api_id' => $val->validated('api_id'), 'api_key' => Model_Api::encryptApiKey($val->validated('api_key')), 'user_id' => $user_id, // <== USER ID )); $api->save(); Model_Api::saveCharacters($api_id, $api_key); Session::set_flash('success', 'Thanks for registering!'); Response::redirect('/'); }
It looks like you're new here. If you want to get involved, click one of these buttons!