Model
enables the commnunication with database using
database relationships. All ORM functionalities of the database model
such as reading and deleting from database can be accessed from database relationships.
This page will focus more on validating and inserting submitted request form data and
since most of the Model class features discussed here are form-related, you can learn more about forms from here.
The following are some of the methods of Model
class and their functions some of which are also integerated with the Form class.
namespace spoova\mi\window\Models; use Model; class SampleModel extends Model { function __construct() { } }
namespace windows\Routes;
use spoova\mi\windows\Models\SampleModel;
use spoova\mi\core\classes\Request;
class Home extends Window {
function __construct() {
$Request = new Request;
$Model = new SampleModel;
if( $Request->isPost() ) {
$Model->loadData($Request->data()); //load form data into model class
}
}
}
Home
url and the form data is submitted, the
Home.php
route class will load the data into the model class. The data can then be processed
based on its required use.
"loadedData()"
is used to retrieve the data supplied into a model class through the laodData
method.
namespace windows\Routes;
use spoova\mi\windows\Models\SampleModel;
use spoova\mi\core\classes\Request;
class Home extends Window {
function __construct() {
$Request = new Request;
$Model = new SampleModel;
if( $Request->isPost() ) {
$Model->loadData($Request->data());
if($Model->validate()) {
//data validated successfully
}else {
print_r($Model->errors());
}
}
}
}
$Model->validate()
is called, the entire loaded data will be validated. If all
validation was successful, then the $Model->validate()
method will return true else it will return false.
If any error occurs, the error will be returned by the error()
method.
error()
method as revealed in the previous example, returns the error messages encountered after validating
each data key. The error returned on each data key is determined by the order of validation rules applied on the corresponding
value of that key.
setError()
method is used to set custom errors. The custom errors are usually stored under the array index key
:mod
. This method takes two arguments of key and value pairs where the first argument is the error name (or key) while the
second argument is the error message. When we call the error()
method, the custom messages can be obtained by first calling the array key :mod
along with the
defined message key. Using $Model->error()
in the earlier code samples, we can obtain custom errors as shown below.
namespace windows\Routes; use spoova\mi\windows\Models\SampleModel; use spoova\mi\core\classes\Request; class Home extends Window { function __construct() { $Request = new Request; $Model = new SampleModel; if( $Request->isPost() ) { $Model->loadData($Request->data()); if($Model->validate()) { //data validated successfully }else { $Model->setError('someError', 'validation failed!'); $modelErrors = $Model->errors(); $customErrors = $modelErrors[':mod'] ?? []; var_dump($customErrors); // ['someError' => 'validation failed!'] } } } }
setError
and error
works together
formdata()
method.
dataUpdate()
method. This is usually done by supplying an array with the data key
and value pairs of data keys that are needed to be updated.
tableName()
is used to return a string of the database table name where database operations are expected to be performed.
saved()
method is usually used to save a data into a database table. It returns true if a data is succesfully saved into the
database. This method is also capable of updating database values immediately before they are inserted into the database fields. Without any lasting
modification of the real loaded data. In order to do this, the first argument must be an array of keys (column name) and value pairs.
true
if data validation is successful and no errors are found.
mapform()
method is used to modify requested data column names. This is usually applicable in situations where
form input data are submitted as the request data. At times, we may need to keep database table field names private because using the same form input name
as database column names may not be advisable due to security issues. The mapform()
method ensures that we can map database columns to html input forms.
This method returns an array list of key and value pairs where the key
is the form input's name and the value
is the corresponding
database field name. This means that value submitted into an html form input's field will be inserted into the relative column name defined.