Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Hooks in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 520
    Comment on it

    Hooks are the functions that we can call prior and afterward doing any task in Models related to database. These are also known as callback methods. Like after finding data, before saving data etc.
    e.g beforeSave(), afterSave(), beforeFind(), afterFind() etc.

    1. beforeSave() :-  
    beforeSave(array $options = array())

    Put any pre-save logic in this function. This function executes instantly after model information has been effectively approved, however just before the information is saved. This function ought to likewise return genuine in the event that you need the save operation to proceed. 

    This callback is particularly convenient for any data-massaging logic that requirements to happen before your information is put away. IIf your storage engine needs dates in a specific format, access it at $this->data and alter it. 

    The following is a sample of how beforeSave can be utilized for date change. The code in the illustration is utilized for an application with a begindate arranged like YYYY-MM-DD in the database and is shown like DD-MM-YYYY in the application. Obviously this can be changed effortlessly. Utilize the code beneath in the fitting model.

    1. public function beforeSave($options = array()) {
          if (!empty($this->data['Event']['begindate']) &&
              !empty($this->data['Event']['enddate'])
          ) {
      
              $this->data['Event']['begindate'] = $this->dateFormatBeforeSave(
                  $this->data['Event']['begindate']
              );
              $this->data['Event']['enddate'] = $this->dateFormatBeforeSave(
                  $this->data['Event']['enddate']
              );
          }
          return true;
      }
      
      public function dateFormatBeforeSave($dateString) {
          return date('Y-m-d', strtotime($dateString));
      }
    2. afterSave() :-  

      If you have logic you should execute it  soon after each spare operation, place it in this callback technique. The saved information will be accessible in $this->data. 

      The estimation of $created will be valid if another record was made (as opposed to a redesign). 

      The $options array is the same one passed to Model::save().

    afterSave(boolean $created, array $options = array())
    

          3. afterFind() :- 

    afterFind(array $results, boolean $primary = false)
    

    Utilize this callback to adjust results that have been come back from a find operation, or to perform whatever other post-find logic. . The $results parameter went to this callback contains the returned results from the model's find operation, i.e. something like:

    $results = array(
        0 => array(
            'ModelName' => array(
                'field1' => 'value1',
                'field2' => 'value2',
            ),
        ),
    );

    4. beforeFind() :- 

    Called before any find-related operation. The $query went to this callback contains data about the present question: conditions, fields, and so on. 

    If you don't wish the find operation to start (possibly based on a decision relating to the $query options), return false. Something else, return the conceivably altered $query, or anything you need to get went to find and its counterparts. 

    You may utilize this callback to confine find operations in view of a client's part, or make caching decisions based on the current load.

    beforeFind(array $query)
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: