Cakephp "action" with multiple functions

Asked

Viewed 57 times

2

I have a few points on how I can improve this "add action" (method) in my "controller":

Some descriptions: In the system the user can upload aProduct in a single form, in the system a Product is composed of Product,Productfeaturese entidades ProductMedias (and 3 different tables in the database).

Question: In the same action(add Controller method Products) i do various tasks (described below) as it would be the best way to separate concepts and operations?

first: I create an entity Product and fill it with form data and custom data, then enter it into the database.

2nd: I create an array ProductFeatures (using a method in a component) each position in the array is another array with the ProductFeatures Key-Value

3º: I created an array ProductFeatures with entities (created from the above array) and then massively insert them all into the database (using a method in a component).

fourth: Upload the uploaded images in the form to a suitable folder (using a method in a component).

5th: I created an array with the data of Pictures and then the tracts so that they have the correct format, then create an entity array (each entity was an array position) then massively insert them all into the database (using a method in a component).

public function add() 
{       
    if ($this->request->is('post')) {

        //Save Product entity
        $product = $this->Products->newEntity();
        $product = $this->Products->patchEntity($product, $this->request->data);
        $product->sub_category_id = 18;
        $product->store_id = 1;
        $productSaved = $this->Products->save($product);

        if($productSaved)
        {
            //Save ProductFeatures entities
            $featuresArray = $this->Insert->getFeatuesArray($this->request->data);
            $featuresEntities = $this->Insert->createMassFeaturesEntities($featuresArray, $productSaved['id']);
            $this->Insert->insertMassEntities($featuresEntities, 'ProductFeatures');

            //Upload pictures to folder in server
            $ROOT_PATH = dirname(ROOT) . DS;
            $PRODUCTS_IMAGES_FOLDER = $ROOT_PATH . 'ShoppingResources' . DS . 'img' . DS . $productSaved['id'];
            $imagesUploaded = $this->UploadFile->uploadFiles($PRODUCTS_IMAGES_FOLDER, $this->request->data['file']);

            //Create and Upload thumbnail to folder in server
            $outputThumb = str_replace($PRODUCTS_IMAGES_FOLDER, $ROOT_PATH . 'ShoppingResources' . DS . 'thumb', $imagesUploaded[0]['url']);
            $imageResized = $this->UploadFile->resizeImage([
                'input' => $imagesUploaded[0]['url'], 'output' => $outputThumb, 'width' => 250, 'height' => 250, 'mode' => 'stretch'
            ]);

            //Prepare image data array to be transformed into entity
            $thumbUploaded['url'] = str_replace($ROOT_PATH, 'http://localhost/PROJETOS/', $outputThumb);
            $thumbUploaded['url'] = str_replace('\\', '/', $thumbUploaded['url']);
            $thumbUploaded['media_type_id'] = 3;

            //Save Media entity (thumbnail)
            $mediaEntity = $this->Insert->createMediaEntity($thumbUploaded, $productSaved['id']);
            TableRegistry::get('Medias')->save($mediaEntity);

            //Prepare image data array to be transformed into entities
            $imagesUploaded = $this->Insert->addKeyValueToArray($imagesUploaded, 'media_type_id', 1);
            $imagesUploaded[0]['media_type_id'] = 2;
            $imagesUploaded = $this->Insert->replaceArrayValue($imagesUploaded, 'url', 'http://localhost/PROJETOS/', $ROOT_PATH);
            $imagesUploaded = $this->Insert->replaceArrayValue($imagesUploaded, 'url', '/', '\\');

            //Save Medias entities
            $mediasEntities = $this->Insert->createMassMediasEntities($imagesUploaded, $productSaved['id']);
            $this->Insert->insertMassEntities($mediasEntities, 'Medias');
        }
    }
}
  • If you create a component you can solve this problem instead of an action

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.