How to set data in request in cake php 3.6

Hii, I am trying to set the data in the request by using $this->request->data[‘mycolumn’] = 34; but getting a warning msg Accessing data as a property will be removed in 4.0.0. Use request->getData() instead. - /var/www/html/rms/src/Controller/JsHandlerController.php, line: 40
** You can disable deprecation warnings by setting Error.errorLevel to E_ALL & ~E_USER_DEPRECATED in your config/app.php. [CORE/src/Core/functions.php, line 312]** So I want to know how to setData in request. So i can proceed to save without errors;

Trying to modify the data for a request is a sign of bad design. What exactly are you trying to do and why do you need to modify the data?

Hi, You did’nt get my point let me explain, I want to set user id in controller when ever form will be submited. In this case i dont want to set user id in form.

You didn’t explain your point, which is why I asked for you to explain exactly why you want to change the request.

The request object is the HTTP request that has resulted in the current controller action being run. It is unchangeable without creating a new request.

For tracking the user id, make use of the existing authentication component and sessions as explained in the documentation (https://book.cakephp.org/3.0/en/controllers/components/authentication.html#accessing-the-logged-in-user)

Hi, you again hadn’t got my point my point. It is not getting the user_id from auth compnent but i have to set that user_id in request to store in the user_id (database field of my table). Prior in cake php 3.5 i had done that by setting the user_id like $this->request->data[‘user_id’] => $this->Auth->user(‘id’); . Now i am using cake 3.6 new release unable to set user_id in request to store in database. It is giving deprecation warning.

That is no longer possible in CakePHP 3.5+ due to the adoption of the PSR-7 standard which requires request objects to be entirely immutable.

There is a highly recommend plugin available that does exactly what you are trying to do without modifying the request object: https://github.com/UseMuffin/Footprint

Alternatively, you could do:

$data = $this->request->getData();
$data['user_id'] = $this->Auth->user('id');
...
1 Like

thanks for answer is appreciable . :slight_smile:
Bdkya

1 Like

What about this way:

$entity= $this->MyModel->patchEntity($entity, $this->request->data);
$entity->user_id = $this->Auth->user('id');
$this->MyModel->save($entity)