I got a few lines of codes in a Model in cakePHP 1.26:
function beforeSave() {
$this->data['User']['pswd'] = md5($raw['User']['pswd']);
return true;
} // this beforeSave() works
The code above has been tested and it is working in my database.
Yet, I am not sure if I can understand it well,
so, I re-wrote the code in other way, and it just failed to work then.
function beforeSave() {
$raw=$this->data;
$raw['User']['pswd'] = md5($raw['User']['pswd']);
return true;
} // this beforeSave() failed to work
Why the second method can not work?
In this line:
You’re just assigning
$this->databy value to$raw. So when you change$raw‘s array data,$this->dataisn’t affected by the change.Besides, you’re totally changing the meaning of your code. What you end up doing is replacing
$raw‘s data with$this->datafrom your model. I’ve not worked with CakePHP before, but I assume$rawalready contains all the raw data you’ve received through some kind input, while$this->datain your model contains the older version of your model data (for example, an older password that the user was going to change). Your changed code will just erase all the new data in$raw, which I don’t think is what you intend to do judging from your first code example.To give you a little explanation of this line:
It’s pretty simple: the
pswditem in theUserarray of$this->datais set as the MD5 checksum of thepswdin theUserarray of$raw.