PHP code example of codekanzlei / cake-model-history
1. Go to this page and download the library: Download codekanzlei/cake-model-history library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codekanzlei / cake-model-history example snippets
$this->addBehavior('ModelHistory.Historizable');
$this->addBehavior('ModelHistory.Historizable', [
'fields' => [
// The field name'firstname' =>[
// Allowed translation forms: String, Closure returning string// Its recommended to use the closure, so translations are made after core initialize when the correct language was set.'translation' => function(){
return __('user.firstname');
},
// The searchable indicator is used to show the field in the filter box// defaults to true'searchable' => true,
// The savable indicator is used to decide whether the field is tracked// defaults to true'saveable' => true,
// obfuscate the values of the field in the history view.// defaults to false except for fieldnames containing "password"'obfuscated' => false,
// Allowed: string, bool, number, relation, date, hash, array, association, mass_association.'type' => 'string',
// Optional display parser to modify the value before displaying it,// if no displayParser is found, the \ModelHistory\Model\Transform\{$type}Transformer is used.'displayParser' => function($fieldname, $value, $entity){
return $value;
},
// Optional save parser to modify the value before saving the history entry'saveParser' => function($fieldname, $value, $entity){
return $value;
},
],
]
]);
/**
* Index action of a controller
*/publicfunctionindex(){
if ($this->request->is(['post'])) {
$entity = $this->Table->newEntity($this->request->data);
$entity->setHistoryContext(ModelHistory::CONTEXT_TYPE_CONTROLLER, $this->request, 'optional_slug');
$this->Table->save($entity);
}
}