beforeSave method not working in cakephp3

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!

@sagubank please give controller code also.

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