Building a Generic Model for the CodeIgniter PHP Framework - Adding getters and setters to the generic model class
(Page 3 of 4 )
In the previous segment, I briefly described the meaning of each property declared within the custom model as well as the basic implementation of its constructor. Now, it's time to extend the model's initial signature by adding to it some additional methods that will be used for defining the name of the associated database table and its corresponding fields, as well as for setting and retrieving the value of the ID field. So, in summary, here's how these brand new methods look. Check them out:
* Gets value of primary key of the associated table for the model
*
* @author Alejandro Gervasio
* @return integer
* @access public
*/
public function getID()
{
return $this->id;
}
As you can see above, the first setter method, called "setTableData()," will assign the name of the database table associated with the generic model to the pertinent $table property, along with its corresponding fields. In addition, it's fair to notice that this method (and others that will be defined in upcoming tutorials) makes use of CodeIgniter's database class, in this case referenced as $this->db. Thus, if you're not familiar with this class, I suggest that you take a look at the CI user guide to learn more about it.
Now, returning to the remaining "setID()" and "getID()" methods, they behave as simple wrappers for setting and retrieving the value of the id of the table row to which the custom model points. Since these are undoubtedly very easy to grasp, I'm not going to waste more of your time explaining how they work.
Well, at this point the custom model looks a bit more functional, even though it's still in an immature state. However, it would be useful to list its complete signature, this time including the methods that you saw before. This process will be accomplished in the section to come, so click on the link below and read the next few lines.