Hardhat Framework

Database

DAOs

In the spirit of favoring simplicity over flexibility Hardhat has some expectations about data access. First, it uses MySQL exclusively, at least for now. It also expects each table to have an unsigned auto increment integer identity column named id.

Database access in Hardhat is done through DAOs (Data Access Objects) which extend the sys_base_dao class.

A typical dao would look something like this:


class core_project_dao extends sys_base_dao
{
   // ---------------------------------------------------- constructor
   public function __construct(){
      parent::__construct('project',core_project_bean, 'project_view');
   }
   public function loadProjects($criteria){
      return $this->loadBeans($criteria);
   }
   public function loadAccountProjects($accountId){
      if (!isset($accountId)) {
          return null;
      }
       return $this->loadBeans("accountId = $accountId");
   }
   public function loadProjectById($projectId){
      return = $this->loadBeanById($projectId);
   }
   public function saveProject(core_project_bean $bean, core_user_bean $u){
      return $this->saveBean($bean, $u);
   }
   public function updateProject(core_project_bean $s, core_user_bean $u){
      return $this->updateBean($s, $u);
   }
   public function deleteProject($id){
      $idao = new core_issue_dao();
      $idao->deleteProjectIssues($id); 
      return $this->deleteBean($id);
   }
}

So let's walk through this. The first thing to note is that the dao extends the sys_base_bean class. It calls the parent constructor passing the table name, the bean name and a view.

Here is the way it works. The view is used to retreive data into the bean. Save, update and delete work on the table. The view is optional, and if not specified the table will be used. The reason we have both is so that we can gather data from more than one table.

For example, it might be nice to have the name of the person who created the project in the bean, if we create a view that joins to the user table, we will get it. But, we will also be able to save the project data back to the project table.

sys_base_dao Class Methods

Here are the methods of the sys_base_dao that you can use:

MethodDescription
loadBeanById($id) Reads the view by the field named id and returns a bean with the fields from the view.
loadBean($criteria, $orderBy = null) Reads the view using the criteria specified, and optionally ordered by the order by criteria. It then returns the first record in a bean.
loadBeans($criteria, $orderBy = null, $limit = null) Reads the view using the criteria specified, optionally ordered by the order by criteria, and optionally limited by the limit criteria. It returns an array of beans loaded from the records selected.
saveBean($bean,$user) Saves the bean to the table, and includes user data in the created by or updated by fields if present on the table. If the id of the bean is not set or is zero, or there is no record on the table for that id, the routine will call insert to insert a record. Otherwise, the record will be updated with the information from the bean.
insertBean(sys_base_bean $bean, $user = null) Inserts the bean to the table, and includes user data in the created by or updated by fields if present on the table.
updateBean(sys_base_bean $bean, sys_user_bean $user) Updates the table with the bean with the information from the bean.
deleteBean($id) Deletes a single bean with the id of $id.
deleteBeans($criteria) Deletes all beans with the specified criteria.