Hi Reader's,
Welcome to FindNerd,today we are going to discuss beforeSave() method in CakePHP.
Basically beforeSave() methods is an important feature of CakePHP and this is a pre-save logic. It is also called callback methods in CakePHP. In this function, we can put any pre-save logic. This function returns true if you want the save operation to continue otherwise it always return false.
let see how it is call
beforeSave(array $options = array())
You can see in below example that how we changed date format by beforeSave method, It means we can see the date in our database like YYYY-MM-DD format but in our application it will be show as DD-MM-YYYY format.
Code will like below:
<?php
public function beforeSave($options = array()){
if (!empty($this->data['User']['joindate'])){
$this->data['User']['joindate'] = $this->dateFormatBeforeSave($this->data['User']['joindate']);
}
return true;
}
public function dateFormatBeforeSave($dateString){
return date('Y-m-d', strtotime($dateString));
}
?>
I hope this blog will help to you for using beforeSave() method in your CakePHP web application.
0 Comment(s)