php - Modify $this->request->data in model CakePHP? -


how can modify $this->request->data model in cakephp. tried code in model user :

public function beforevalidate($options = array()) {     unset($this->request->data['user']['birthday']); } 

but return errors :

notice (8): indirect modification of overloaded property user::$request has no effect

warning (2): attempt modify property of non-object

if use (model user) :

public function beforevalidate($options = array()) {     unset($this->data[$this->alias]['birthday']); } 

it's ok, after validate, when tried print_r($this->request->data) in controller, see birthday field still exists in it.

anyone can give me solution this, different between $this->data , $this->request->data, !!

edit : cakephp version 2.6.7 - newest version.

$this->request->data cannot accessed within model. data accessible controller. when attempt save data model controller (e.g. $this->user->save($this->request->data))) setting user model's data attribute. in other words, happening:-

$this->user->data = $this->request->data; 

so in model's callback methods can access data being saved using $this->data , manipulate have found in beforevalidate():-

public function beforevalidate($options = array()) {     // unset 'birthday' data being saved     unset($this->data[$this->alias]['birthday']);     return parent::beforevalidate($options); } 

don't forget when using callback call parent method , ensure returns boolean. if doesn't return true data not saved!

if manipulate $this->data in model not affect $this->request->data can access model's data attribute within controller see changes. example, in controller after saving changes:-

// output user data debug($this->user->data); 

if want alter $this->request->data need within controller (presumably before save), not model:-

unset($this->request->data[$this->alias]['birthday']); 

just side note careful of unsetting data in model callback's everytime try save data (unless disable callback). unsetting birthdaywould result in never getting saved database.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -