# How to save images in cakephp 3.4.0 with cakephp-upload while being immutable

**URL:** <https://discourse.cakephp.org/t/how-to-save-images-in-cakephp-3-4-0-with-cakephp-upload-while-being-immutable/6515>\
**Category:** Need Help\
**Created:** [August 19, 2019, 4:56pm UTC](https://discourse.cakephp.org/t/how-to-save-images-in-cakephp-3-4-0-with-cakephp-upload-while-being-immutable/6515 "2019-08-19T16:56:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haradwaith](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@Haradwaith](https://discourse.cakephp.org/u/Haradwaith)\
**Post date:** [August 19, 2019, 4:56pm UTC](https://discourse.cakephp.org/t/how-to-save-images-in-cakephp-3-4-0-with-cakephp-upload-while-being-immutable/6515/1 "2019-08-19T16:56:43Z")

</div>

Hi,

I’m working on my cakephp project  
and I am currently upgrading from 3.3.16 to 3.4.0

The project uses the [cakephp-upload plugin](https://github.com/FriendsOfCake/cakephp-upload) to save an image.  
The Upload plugin needs an existing entity to attach a file to it. A modification of the request is done to grab the avatar, before unsetting it to save the user.

I know this is not a good practice to modify a request, but the code was made this way.  
With immutable objects in version 3.4.0, it is just not possible anymore. But i dont know how to do it properly.

Here is the error message given by my unit-test,  
ran by `vendor/bin/phpunit --filter testAdd tests/TestCase/Controller/Api/V1/UsersControllerTest.php`:

```auto
There was 1 failure:

1) App\Test\TestCase\Controller\Api\V1\UsersControllerTest::testAdd
Failed asserting that file "/home/comptoir/Comptoir-srv/webroot/img/files/Users/photo/5/avatar/correctAvatarLogo.jpg" exists.

/home/comptoir/Comptoir-srv/tests/TestCase/Controller/Api/V1/UsersControllerTest.php:208

```

Here is the actual code:

```auto
public function add()
    {
        if (!empty($this->request->data)) {
            $user = $this->Users->newEntity($this->request->data);
        } else {
            $user = $this->Users->newEntity();
        }

        $message = "";
        // Get the avatar before unset it to save the user.
        // The Upload plugin need an existing entity to attach a file to it.

        if ($this->request->is('post')) {
            if (isset($this->request->data['photo']) && !$user->errors()) {
                $avatar = $this->request->data['photo'];
                $this->request->data['photo'] = "";
            }

            $user = $this->Users->patchEntity($user, $this->request->data);

            if ($this->Users->save($user)) {
                $user = $this->Users->get($user->id, ['contain' => []]);

                isset($avatar) ? $this->request->data['photo'] = $avatar : null;

                $user = $this->Users->patchEntity($user, $this->request->data);

                if ($this->Users->save($user)) {
                    $message = "Success";
                    $this->Flash->success(__d("Forms", "Your are registred on the Comptoir du Libre, welcome !"));
                    if (!$this->request->is('json')) {
                        $this->Auth->setUser($this->Auth->identify());
                        $this->redirect([
                            "prefix" => false,
                            "controller" => "Pages",
                            "language" => $this->request->param("language")
                        ]);
                    }
                } else {
                    $message = "Error";
                }
            } else {
                $message = "Error";
                $this->Flash->error(__d("Forms", "Your registration failed, please follow rules in red."));
            }

            $message == "Error" ? $this->set('errors', $user->errors()) : null;
        }

        $this->ValidationRules->config('tableRegistry', "Users");
        $rules = $this->ValidationRules->get();
        $userTypes = $this->Users->UserTypes->find('list', ['limit' => 200]);
        $this->set(compact('user', 'userTypes', 'rules', 'message'));
        $this->set('_serialize', ['user', 'userTypes', 'rules', 'message', 'errors']);
    }

```

Does anyone know how to do that respecting the immutable rule ?

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [August 19, 2019, 10:35pm UTC](https://discourse.cakephp.org/t/how-to-save-images-in-cakephp-3-4-0-with-cakephp-upload-while-being-immutable/6515/2 "2019-08-19T22:35:22Z")

</div>

> [@Haradwaith](#):
>
> ```php
> if (isset($this->request->data['photo']) && !$user->errors()) {
> $avatar = $this->request->data['photo'];
> $this->request->data['photo'] = ""; 
> }
> 
> ```

This would be converted to

```php
if (isset($this->request->getData('photo')) && !$user->errors()) {
    $avatar = $this->request->getData('photo');
    $this->request = $this->request->withData('photo', '');
}

```
