# beforeSave method not working in cakephp3

**URL:** <https://discourse.cakephp.org/t/beforesave-method-not-working-in-cakephp3/8531>\
**Category:** Need Help\
**Tags:** help\
**Created:** [October 21, 2020, 7:17am UTC](https://discourse.cakephp.org/t/beforesave-method-not-working-in-cakephp3/8531 "2020-10-21T07:17:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagubank](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@sagubank](https://discourse.cakephp.org/u/sagubank)\
**Post date:** [October 21, 2020, 7:17am UTC](https://discourse.cakephp.org/t/beforesave-method-not-working-in-cakephp3/8531/1 "2020-10-21T07:17:41Z")

</div>

I have data that I want to modify first before saving it to my database, and so I’ve researched the beforeSave method.

I have a user’s picture input and I want to save its path on my DB after successfully validating it and here’s my current code:

```
    src/model/table/UsersTable.php

    use Cake\ORM\Entity;
    use Cake\Event\Event;
    use ArrayObject;
    use Cake\Validation\Validator;

    public function validationDefault(Validator $validator)
    {            
       $validator
            ->allowEmptyFile('image_location')
            ->add('image_location', 
            [
                'mimeType' => [
                    'rule' => array('mimeType', array( 'image/png', 'image/jpg', 'image/jpeg')),
                    'message' => 'Please upload images only (png, jpg).'
                ],
                'fileSize' => [
                    'rule' => array('fileSize', '<=', '10MB'),
                    'message' => 'Image must be less than 10MB.'
                ],
            ]);
                        
        return $validator;
    }

    public function beforeSave($event, $entity, $options)
    {
        if ($entity->image_location['name']) {
            $tmp = $entity->image_location['tmp_name'];
            $hash = rand();
            $date = data("Ymd");
            $image = $dage.$hash;
            $target = WWW_ROOT.'img'.DS.'uploads'.DS;
            $target = $target.basename($image);
            $image_location = "uploads/".$image;
            $entity->image_location = $image_location;
            move_uploaded_file($tmp, $target);
        }
    }

```

The only working part on this is the validation part, but after it successfully validates the image file, the beforeSave method is not working.

What error do I have in my current code or how can I use the beforeSave method in cakephp3.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![Hareshpatel](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@Hareshpatel](https://discourse.cakephp.org/u/Hareshpatel)\
**Post date:** [October 21, 2020, 2:29pm UTC](https://discourse.cakephp.org/t/beforesave-method-not-working-in-cakephp3/8531/2 "2020-10-21T14:29:47Z")

</div>

@sagubank please give controller code also.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [October 21, 2020, 3:37pm UTC](https://discourse.cakephp.org/t/beforesave-method-not-working-in-cakephp3/8531/3 "2020-10-21T15:37:10Z")

</div>

> [@sagubank](#):
>
> the beforeSave method is not working.

Not working as in “never gets run” or “throws the error xxx” or “produces the wrong result xxx”?
