I have some questions on how I can improve this add action method in a controller.Using a single form, the user can upload a product to be displayed on the site.  A Product is composed of three entities: Product, ProductFeatures and ProductMedias (stored in three database tables).To prepare data for insertion and upload, I have created two helper classes with static methods to simplify the controllers. (This approach should help with rollback if necessary).In one action (the add method of Products controller), I do multiple tasks:The setProductByForm method in ProductsTable creates an empty Product entity, populates it with form data, and saves it in the database.public function setProductByForm($form){     $product = $this->newEntity();     $product = $this->patchEntity($product, $form);     return $this->save($product);}The uploadFiles method in UploadFilesComponent stores the common images.A method in the ModelUtils class calls other methods that prepare and tranform the data in entities.public static function prepareProductsFeatures($formValues, $productId){    $productFeaturesArray = self::createProductsFeaturesArray($formValues);    $productFeaturesEntities = self::createProductsFeaturesEntitiesArray(        $productFeaturesArray, $productId    );    return $productFeaturesEntities;}I create the URL where thumbnail will stored (using a method in the UploadUtils class).public static function getOutputThumbUrl($mainImage, $productId){    return str_replace(PRODUCTS_IMAGES_FOLDER . $productId,        PRODUCTS_THUMB_FOLDER, $mainImage);}The thumbnail is stored.Methods in ModelUtils transform data into entities and save them in MediasTable.What is the best way to separate concepts and operations?Note: I've tried to use afterSave but it's impossible to access request->data from it.Controller actionpublic function add(){    $this->autoRender = false;    if ($this->request->is('post'))    {        $productSaved = $this->Products->setProductByForm($this->request->data);        if($productSaved)        {            $this->loadModel('ProductFeatures');            $productFeaturesSaved = $this->ProductFeatures->setProductFeaturesEntities(                ModelUtils::prepareProductsFeatures($this->request->data, $productSaved['id'])            );            $imagesUploaded = $this->UploadFile->uploadFiles(                PRODUCTS_IMAGES_FOLDER . $productSaved['id'], $this->request->data['file']            );            $outputThumbUrl = UploadUtils::getOutputThumbUrl($imagesUploaded[0]['url'], $productSaved['id']);            $thumbUploaded = $this->UploadFile->resizeImage([                'input' => $imagesUploaded[0]['url'], 'output' => $outputThumbUrl,                'width' => 250, 'height' => 250, 'mode' => 'stretch'            ]);            $this->loadModel('Medias');            $thumbSaved = $this->Medias->setMediaEntity(                ModelUtils::prepareMediaThumb($productSaved['id'], $outputThumbUrl)            );            $mediasSaved = $this->Medias->setMediasEntities(                ModelUtils::prepareMedias($imagesUploaded, $productSaved['id'])            );        }    }}Full source codeProductsController classModelUtils classUploadUtils classProductsTable classMediasTable classProductFeaturesTable class
Controller method to add a product, stored across three database tables
php;database;controller;atomic;cakephp