Skip to content

Instantly share code, notes, and snippets.

@eberfreitas
Created April 2, 2015 02:38
Show Gist options
  • Save eberfreitas/e6e6d6755b59ed2e2fc1 to your computer and use it in GitHub Desktop.
Save eberfreitas/e6e6d6755b59ed2e2fc1 to your computer and use it in GitHub Desktop.
Simple field setters for CakePHP 2.X.X
<?php
class AppModel extends Model {
public function beforeValidate($options = array()) {
$this->_setupSetters();
return true;
}
protected function _setupSetters() {
foreach ($this->data[$this->alias] as $key => $value) {
$methodName = '_set' . Inflector::camelize($key);
if (method_exists($this, $methodName)) {
$this->data[$this->alias][$key] = call_user_func([$this, $methodName], $value);
}
}
}
<?php
class User extends AppModel {
public $displayField = 'name';
protected function _setName($value) {
return str_rot13($value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment