Defining Terms For Model Methods - axiom82/PHP-Contract GitHub Wiki
It is often overlooked, but it is indeed mission-critical to certify the data being passed into model method arguments. Below is the simplest of checks, ensuring that an ID is passed through the first parameter, in order to select a row from a database table. In this case, a table containing passes.
<?php
public function getPass($passId){
$contract = new Contract();
$contract->term('passId')->id();
$contract->metOrThrow();
/* The Contract Has Been Met, Get The Pass From The Database */
$select = $this->_db->select()
->from(array('p' => 'pass'), '*'))
->where('p.id = ?', $passId);
$row = $this->_db->fetchRow($select);
return $row;
}
?>The Contract above checks to see if the $passId argument is indeed an ID type. If the $passId argument is valid, then the model method has the parameters required to get the pass record for the database. Again, this is the simplest of circumstances in creating terms for model methods. Many times, you will want to check multiple arguments for types and values, even creating custom terms of your own to ensure that all of the requirements for data requests are properly prepared prior to making calls to your database.
It’s about precision in application technology.
In the next tutorial, we will be going over another intermediate example in using PHP-Contract, Securing A Database Record Update.