Record Objects

Now that we know how to query for data, you'll probably want to know how to load, save and delete individual rows.  To do this you will use Record Objects.

 

A Record Object is an object which represents one row in the database.  At the risk of using too much technical jargon, Record Objects are, in many ways, Beans objects which also implement the Active Record design pattern.

 

Before we get too far, let's define what a "Bean" is.  A Bean is traditionally a component which encapsulates its instance data in private properties.  It then provided "getters and setters" (assessors and mutators, technically) which are used to read and write those values.

 

Did you get all that?  I'll simplify it a bit, just in case: Our User table has six fields.  When we create a Record object it will have six pairs of methods for reading and setting the values of those fields.  

 

For instance, one field is username.  So, we'll have a method getUsername() which returns the username.  We'll also have a method called setUsername(username) which accepts a username and sets the value within the object.  If you pass "Doug" into setUsername(), subsequent calls to getUsername() will return Doug.

 

Traditionally, a bean only has getters and setters and nothing more.  However, I also said that the Record objects implemented the Active Record design pattern.  This really means that Record objects know how to persist themselves to the database.

 

Note: There are a few interpretations of the Active Record design pattern.  This is not exactly the same as the implementation in Ruby on Rails!

 

To facilitate this, the Record objects have three important methods: load(), save() and delete().  They are very descriptive of what they do.

 

I suspect that by now you want to know how to Read a Record.