Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:08:39+00:00 2026-05-29T23:08:39+00:00

I am working on a small application to provide quotes for custom products. It’s

  • 0

I am working on a small application to provide quotes for custom products. It’s my first cakePHP application.

Many of the fields for the products are calculated automatically when a product is added or saved. The calculations use valuse stored in the ‘Rates’ table to perform the operations. These ‘Rates’ can also be updated by the admin and have thier own model, view and controller. However, when the Rates are updated I need all of the existing products to be re-calculated as if the user had been to /products/edit and clicked save.

I really don’t know how to trigger this when the rates are saved to the database

here is my ProductsController edit function:

public function edit($id = null) {
    $this->Product->id = $id;
    if (!$this->Product->exists()) {
        throw new NotFoundException(__('Invalid product'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        $this->loadModel('Rate', '1');

        $Od = $this->request->data['Product']['material_od'] / 2;
        $materialMass = $this->Rate->field('steel_mass') * $this->request->data['Product']['material_length'] * (pi() * $Od * $Od );
        $this->Product->saveField('material_mass', $materialMass);

        $materialCost = $materialMass * $this->Product->Material->field('cost', array('Material.id' => $this->request->data['Product']['material_id']));
        $this->Product->saveField('material_cost', $materialCost);

        $materialMarkupRate = $this->Rate->field('material_markup') + 1;
        $wasteMarkupRate = $this->Rate->field('waste_markup') + 1;

        $materialMarkupCost = $materialCost * $materialMarkupRate * $wasteMarkupRate;
        $this->Product->saveField('material_markup_cost', $materialMarkupCost);

        $setupCost = $this->request->data['Product']['number_tools'] * $this->Rate->field('tool_time') * $this->Rate->field('setup_rate');
        $this->Product->saveField('setup_cost', $setupCost);

        $cuttingCost = $this->request->data['Product']['cutting_time'] * $this->Rate->field('cutting_rate');
        $this->Product->saveField('cutting_cost', $cuttingCost);

        $machiningCost = $this->request->data['Product']['machining_time'] * $this->Rate->field('machining_rate');
        $this->Product->saveField('machining_cost', $machiningCost);

        $polishingCost = $this->request->data['Product']['polishing_time'] * $this->Rate->field('polishing_rate');
        $this->Product->saveField('polishing_cost', $polishingCost);

        if ($this->Product->save($this->request->data)) {
            $this->Session->setFlash(__('The product has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The product could not be saved. Please, try again.'));
        }
    } else {
        $this->request->data = $this->Product->read(null, $id);
    }
    $materials = $this->Product->Material->find('list');
    $this->set(compact('materials'));
}

and my RatesController edit function:

public function edit($id = null) {
    $this->Rate->id = $id;
    if (!$this->Rate->exists()) {
        throw new NotFoundException(__('Invalid rate'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->Rate->save($this->request->data)) {
            $this->Session->setFlash(__('The settings have been saved.  Please update your products.'));
            $this->redirect(array('controller' => 'products', 'action' => 'index'));
        } else {
            $this->Session->setFlash(__('The rate could not be saved. Please, try again.'));
        }
    } else {
        $this->request->data = $this->Rate->read(null, $id);
    }
}

How can i trigger the first one from the second one?

I’m fairly new to this so all tips suggestions and criticism is very welcome!

Many Thanks,
Ralph

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:08:40+00:00Added an answer on May 29, 2026 at 11:08 pm

    There are a couple of issues that will help you not only clear up what you are trying to do, but do it in a more efficient way. Currently, every time you call $this->Product->saveField you are hitting your database. If this is a small app used by few people, it won’t matter much. However, you should get into the habit of writing good clean code. Your variable names are great! Easy to understand. I would first suggest calculating all of the values and then hitting the database like this:

    ...
        $this->loadModel('Rate', '1');
        $materialMarkupRate = $this->Rate->field('material_markup') + 1;
        $wasteMarkupRate = $this->Rate->field('waste_markup') + 1;
    
        $Od = $this->request->data['Product']['material_od'] / 2;
        // make sure you have a hidden id form in the view, otherwise you will need to set it here
        $this->request->data['Product']['material_mass'] = $this->Rate->field('steel_mass') * $this->request->data['Product']['material_length'] * (pi() * $Od * $Od );
        $this->request->data['Product']['material_cost'] = $materialMass * $this->Product->Material->field('cost', array('Material.id' => $this->request->data['Product']['material_id']));
        $this->request->data['Product']['material_markup_cost'] = $materialCost * $materialMarkupRate * $wasteMarkupRate;
        $this->request->data['Product']['setup_cost'] = $this->request->data['Product']['number_tools'] * $this->Rate->field('tool_time') * $this->Rate->field('setup_rate');
        $this->request->data['Product']['cutting_cost'] = $this->request->data['Product']['cutting_time'] * $this->Rate->field('cutting_rate');
        $this->request->data['Product']['machining_cost'] = $this->request->data['Product']['machining_time'] * $this->Rate->field('machining_rate');
        $this->request->data['Product']['polishing_cost'] = $this->request->data['Product']['polishing_time'] * $this->Rate->field('polishing_rate');
    
        if ($this->Product->save($this->request->data)) {
    ...
    

    Second, if you need to call this method from other locations, that is a sign that you need to move it. Because this is data, you should move it to the Model.

    However, if you are saying that everytime the rates change, EVERY product in the database needs to be updated, this is still going to be a problem. Maybe not now, but later down the road. Think about having a 100MM records. You change one thing on the rates and you are churning on the database for a LONG time. That’s a lot of resources.

    My advice is the change this and move it to the onFind callback in the ProductModel. The way it will work is every time you pull a record, it will run the math on the values before they are returned to the view. It is normally best NOT to store calculated values in the database. There are certain circumstances where you will want to, but in this case it seems you can calculate them on the fly. This will also prevent the need to recalculate every product every time the rate changes. It will also make for cleaner code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a small server application in C# which should provide a VT100/ANSI
Im working on a small application to try out an idea that I have.
I am working on a small application in VB.NET. The program needs administrator privilege
I'm working on a small application and thinking about integrating BLAST or other local
I have a small application I am working on that at one point needs
I am working on a small MFC application..since am new to MFC I am
I'm currently working on a small web application using Visual Studio 2008 Express. I'm
i have developed a small application and was working fine on developing machine but
I am currently working on a small sponsorship application(PHP/MySql) for my personal blog, and
I'm working on writing a small application using the WPF MediaKit , and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.