DAO

Data Access Objects are subclasses of one of the core DAO classes. They facilitate the data exchange in between the application and the database.

DAO's lie on top of the database layer; a set of classes that specialize in a single database. This DB layer helps improving isolation allowing developers to use DAO objects without caring about SQL.

Implementing DAO's in your application

To implement a specific DAO you just need to subclass it and define some simple properties

Example:

1 <?php 2 class photos_DAO extends DAO { 3 protected $table = "T_PHOTOS"; 4 public $keyField = "I_PHOTOID"; 5 protected $classType = "photos_VO"; 6 ?>

* You will normally need one DAO per Table in your database

This photos_DAO inherits all the DAO functionality, including transaction support. Use this list of methods for reference, or check the documentation for more information:

    • count(String $criteria) Long

    • delete(object &$vo) unknown

    • deletebyid(Long $id) boolean

    • exist(String $criteria) Boolean

    • find(String $criteria = "", Long $start = 0, Long $limit = 30, String $orderby = '') VO

    • findSingle(String $criteria) VO

    • get(Long $id) VO

    • get_new() VO

    • insert(&$vo) void

    • removenullable(Object &$vo) void

    • save(&$vo, $stored = null) unknown

* You might notice there is no update method, as DAO takes care of data integrity it will automatically decide in between insert and update when you call save.

Extending DAO

It's possible and very easy to extend the DAO class you simply need to add the function straight in your class. Please be careful about which methods goes into the DAO class and which methods into the Model class.

    1. You should only place methods that relate straight to data without any extra business rules

    2. Try not to create alternative methods for existing ones, instead try overriding the function you want to enrich/modify

    3. All SQL must use the defined varialbles (i.e. Table)

    4. SQL must be generic (No SQL flavors)

    5. Limit clauses mysql style are allowed as the DB layer will transform for specific databases.

We recommend you to use getFromResult or getvoarray internal functions to convert your SQL statement, alternatively you can use getData method passing the right VO type as the second parameter. More on data

DAO types

Depending on your database design you need to inherit from a different specialized DAO, the available types are:

    • DAO

    • DAO_blob

    • DAO_service

    • DAO_unique

DAO

Most applications using auto increment fields will inherit DAO class

DAO_blob

For tables with auto increment key and BLOB fields normally images, files or other binary storage or long text. While in MySql this fields are automatically retrieved using a normal select, Oracle requires special code to access them.

Note. Currently DAO_blob will return VO objects with the BLOB data, in the future this behavior will be changed so objects will NOT return this BLOB fields and an extra call will have to be done i.e. $dao->getBLOB($vo); This change is high priority.

DAO_unique

Use this DAO when no autonumeric key exist

Specialized DAO class for tables with combined keys i.e. Key = req_no + test_no

* Please notice that the variable $key disappears, instead $keys is used with type array

DAO_service

Provides an object model for data structures that doesn't match a table Row. This DAO provides methods to access data but not for saving, you should implement this method when you have complex inner joins.

* Alternatively you can make an independent model and use function getData with only your SQL as parameter.

What's the advantage or using a DAO instead of just calling getData in my application?

The short answer: The reusablity of your code will improve.

Your application will be easier to maintain and the code will be properly organized, DAO will also take care of some nasty details of database specific issues, like nullable fields, non-sql compliant delimiters for some fields, etc.

If in the future you need to replace the DB it will be easier to maintain your code and to adjust the DAO for any specific issue you find.