Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Change Data Before Saving to DB

Tags:

php

cakephp

So I have the object $this->data['VideoForm']['filename'] in the controller, but I want to append the return value of getExtension($filename) to it before I save it to the database. This is what I was trying:

$this->data['VideoForm']['filename'] = $this->data['VideoForm']['filename'] . "." . getExtension($this->data['VideoForm']['file']['name']);

It throws an error when I try to assign it a new value with the "=" or "=>" operator. Any idea of how to do it? I'm sure it's something very simple that I'm missing...

EDIT: This is the error:

Notice (8): Indirect modification of overloaded property MediaController::$data has no effect [APP/Controller/MediaController.php, line 31]

EDIT: I've continued looking around, and the getExtension() function is definitely returning "jpg" if I upload xxx.jpg, so that's not the problem. Any ideas?

like image 497
K. Barresi Avatar asked Jun 12 '26 04:06

K. Barresi


1 Answers

That error is due to PHP 5.2+ change in the way __get() works -- it doesn't return a reference to the value, instead a read-only version. Cake 2.0+ uses __get() for some controller properties.

The solution in this case is to write to $this->request->data instead of $this->data.

Ref: Mark Story's post on Google groups

like image 175
Costa Avatar answered Jun 15 '26 23:06

Costa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!