DAO & DTO's
So with the hard work of making the model and the controller behind, let's focus on the DAO and the VO
Notice: DTO were formerly called VO
Naming standards and casing
As a useful convention, DAO and VO should have the same name with a different prefix, in this case categories (you can also call them after the table t_categories)
Case is critical for database switching, we recommend using capital letters always to refer to tables and fields remember PHP is case sensitive!
DAO
Data Access objects, allow us to keep the database code out of the model and to avoid repeating statements. They also help when switching database types.
The DAO will contain the methods and information to map the objects to the DB.
Let's create the file:
includes/app/dao/categories_DAO.php
DAO Code
1 <?php 2 class categories_DAO extends DAO { 3 protected $table = "T_CATEGORIES"; 4 protected $keyField = "I_IDCATEGORY"; 5 protected $classType = "categories_VO"; 6 } 7 ?>
More on DAO
There's more on DAO's that meet the eye, we will add soon more information on the other DAO's
DAO_unique: For not auto numeric keys
DAO_blob: For VO's that contain pictures or very long text
DAO_service: For VO's that doesn't map to DB
VO
Value Objects are design pattern used to transfer data between software applications or layers.
Normally they represent a single row in the database but they can contain any data structure
Let's create the file:
includes/app/VO/categories_VO.php
VO Code
1 <?php 2 class categories_VO{ 3 public $I_IDCATEGORY, $I_NAME; 4 } 5 ?>
And that's it.. just add one variable for each field in your table.
How to use the VO's?
When you use the DAO to get information from the database you will normally get a VO object, you can access it's properties like any object:
$myobject->myproperty;
Does VO's make sense? why not using parameters?
Perhaps you cannot see the advantage of creating and passing an object with just an id and a name but it sure make sense as the object properties multiply, while arrays could do the job VO's give us greater control and more possibilities.
Done! Take me to template!
More on DB? go to access data page, or read more about DAO