PHP hooks information and help needed
        Posted  
        
            by sea_1987
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by sea_1987
        
        
        
        Published on 2010-04-09T08:45:52Z
        Indexed on 
            2010/04/09
            9:03 UTC
        
        
        Read the original article
        Hit count: 343
        
Background
I am realtively new to hooks, and I have been asked to use a hook to populate a view with some data, currently the view gathers it data from a function that is in the model, and becuase the whole object is being passed to the view I can access the function.  The function looks like this,
    public function numCVInSector($k) {
  $this->load->model('SYSector');
  $sectorModel = new SYSector();
  $cvs = $sectorModel->fetchRelatedCV($k);
  $cvs = $cvs[0]['count'];
  if($cvs == "0") {
   return false;
  } else {
   return $cvs;
  }
 }
it call's a query in the model that looks like this,
  public function fetchRelatedCV($k) {
  $sql = "SELECT sector_id, COUNT(sector_id) as count FROM sy_user_sectors WHERE sector_id = $k";
  //print_r($sql);
  $query = $this->db->query($sql);
  return $query->result_array();
 }
$k is the id of element that is in the view.
The Problem
I have been asked to use a hook that is in the parent of the model the hook is called post populate, now I have very little idea of what a hook is or how to use one to implement my function.  Could some one give me some advice, the code where the hook is original made looks like this, 
 public function populate($where = array())
{
    $results = array(
        "success"       => false,
        "is_error"      => false,
        "error_code"    => "",
        "error_message" => ""
    );
    if(empty($where) && empty($this->aliases['id'])){
        $results['is_error'] = true;
        $results['error_message'] = 'No criteria.';
        return $results;
    }
    // [hook]
    $this->prePopulate();
    $where = count($where) > 0 ? $where : array('id' => $this->aliases['id']);
    $query = $this->db->get_where($this->tableName, $where, 1);
    if($query->num_rows() < 1){
        $results['error_message'] = 'Empty results.';
        return $results;
    }
    foreach($query->result_array() as $row){
        foreach($this->aliases as $key => $val){
            $this->aliases[$key] = $row[$key];
        }
    }
     // [hook]
    $this->postPopulate();
    // Presume success
    $results['success'] = true;
    return $results;
}
I have been asked to use the postPopulate hook.
public function postPopulate()
{
    $args = $this->getHookArgs('post_populate');
    if(!is_array($args)){
        // $this->fb->log($args, 'bad args');
        return false;
    } 
    // code here...
    // Convert dates to front end formats.
    foreach($this->frontEndDateFields as $fieldName => $dateFormat){
        $dateRes = mlib_du_getFormattedMySQLDate($this->aliases[$fieldName], $dateFormat);
        if($dateRes != false){
            $this->aliases[$fieldName] = $dateRes;
        } 
    }
    return true;
}
© Stack Overflow or respective owner