Model class has been improved with new methods which are id(), insertID() and
connection(). The insertID() returns the insertion id generated when data is inserted to the database. The
id() method returns the id value of an inserted data that is equivalent to the defined id database table column's
name. The connection() method returns the current database connection instance. Since the Form class are used along
with model class, each of these methods can be statistically called on the Form class (e.g Form::id()).
namespace spoova/mi/windows/Model;
class SignupModel {
public $firstname;
public $lastname;
public $username;
public $password;
function rules() : array {
return [
'firstname' => [SELF::REQUIRED],
'lastname' => [SELF::REQUIRED],
'username' => [SELF::REQUIRED, SELF::MIN => '5'],
'password' => [SELF::REQUIRED, SELF::MIN => '8'],
];
}
function isAuthenticated() : bool {
//get request password
$password = Form::datakey('password');
//get request password hash
$passHash = password_hash($password, PASSWORD_DEFAULT);
//save data into database by overwriting request password with hashed value
return Form::isSaved([ 'password' => $passHash ]);
}
}
id() method returns the id of a user by either searching for the configured USER_ID_FIELDNAME
in the request data list or the last inserted database id. Assuming that we have the USER_ID_FIELDNAME field configured as "username" in the
"icore/init" file, In order to register a user into database when a form is submitted, a similar procedure
like the approach below will employed
namespace spoova/mi/windows/Routes;
use User;
class Signup extends Windows {
function __construct(Request $Request) {
if($Request->isPost()) {
$SignupModel = new SignupModel;
Form::model($SignupModel); //set model
Form::loadData( $Request->data() );
if( Form::isAuthenticated() ) {
//fetch username from saved data
$userid = $SignupModel->id();
//create a new session using userid
User::login(['userid' => $userid]);
}
}
}
}
Form::model()->id()
or better still Form::id().
namespace spoova/mi/windows/Routes;
use User;
class Signup extends Windows {
function __construct(Request $Request) {
if($Request->isPost()) {
Form::model(new SignupModel); //set model
Form::loadData( $Request->data() );
if( Form::isAuthenticated() ) {
//create a new session using userid
User::login(['userid' => Form::id()]);
}
}
}
}
Form class using Form::model(). The
request data is loaded for validation into the Form class using the Form::loadData()
method. By default, the Form::isAuthenticated() returns true only if the required request data
are successfully validated. However, this method is also capable of calling the supplied model's "isAuthenticated()"
method. By making use of this advantage, we can save the form into the database using the Form::isSaved() method within the
declared model's, that is, SignupModel->isAuthenticated()
method. This means that Form::isAuthenticated() will both authenticate the request data and also try to save them
into database if authentication was successful. The Form::id() method which is relative to the Model's id()
method will either return the configured USER_ID_FIELDNAME value found in request data or the autogenerated last inserted id.
insertID() method is only used to obtain the last inserted id of a data inserted into the database table. This is useful in
situations where the last inserted id is required even if the configured USER_ID_FIELDNAME exists as a different key in the request data.
namespace spoova/mi/windows/Routes;
use User;
class Signup extends Windows {
function __construct(Request $Request) {
if($Request->isPost()) {
Form::model(new SignupModel); //set model
Form::loadData( $Request->data() );
if( Form::isAuthenticated() ) {
//fetch last inserted id if it exists.
$insertID = Form::model()->insertID();
//create a new session using userid
User::login(['userid' => Form::id()]);
}
}
}
}
connection(), also Form::connection() returns the current database handler connection used by the model.
namespace spoova/mi/windows/Routes;
use User;
class Signup extends Windows {
function __construct(Request $Request) {
if($Request->isPost()) {
Form::model(new SignupModel); //set model
vdump(Form::connection()); //fetch current connection
}
}
}