• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Models Remember Vars?

Hi there guys, sorry been abusing the forum a bit here just trying to get use to using these models seem to be causing me more trouble at the moment (my fault Smile). Has anyone had any problems with using a model then changing the variables and trying to do another query... Id you look in my code below I update the product parts table but I then need to access it again to calculate the new prices for the product. For some reason though it remembers the type setting which I don't want it to because I need it to recalculate everything not just the raw material parts. Is there a way to flush the model so it forgets about my previous settings or does it not actually remember it and I am actually doing something more stupid?



//Update All Product Tables so they have new pricing of raw material
        $this->mdl_product_parts->type = "raw_material";
        $this->mdl_product_parts->id_of_selected_type = $_POST['raw_materials_id'];
        $this->mdl_product_parts->price = $_POST['raw_materials_unit_price'];
        $ids = $this->mdl_product_parts->get();
        foreach($ids as $row)
            $this->load->model("mdl_product_parts"); //Load product parts model
            $this->mdl_product_parts->product_id = $row->product_id; //set the product ID so we know what product parts to fetch    
            $this->mdl_products->product_id = $row->product_id;
            $percentageID = 0;
            //find percentage ID
            $result = $this->mdl_products->get();
            foreach($result as $r)
                $percentageID = $r->product_percentage_id;
            //Find Percentage
            $this->mdl_percentage->percentage_id = $percentageID; //Set percentage ID to what was set on the edit products page
            $percentage = 0;
            $result = $this->mdl_percentage->get();
            foreach($result as $row)
                $percentage = $row->percentage_value; //Set the percentage Varible to the percetnage value so we know how much markup.
            $result = $this->mdl_product_parts->get();
            $total = 0;
            $laborHours = 0;
            $laborCost = 0;
            $materialCost = 0;
            $markup = $percentage;
            foreach($result as $row)
                $temp = $row->price * $row->qty_of_selected_type;
                $total += $temp;
                //work out labor hours and cost
                if($row->type == "labor")
                    $laborHours += $row->qty_of_selected_type;
                    $laborCost += ($row->qty_of_selected_type * $row->price);
                if($row->type == "raw_material")
                    $materialCost += ($row->qty_of_selected_type * $row->price);
            $sellingPrice = $total * $markup / 100;
            $sellingPrice += $total;
        $this->mdl_products->manual = "0"; //Manual is false becuase percentage method has been used
        $this->mdl_products->product_material_cost = $materialCost;
        $this->mdl_products->product_labor_cost = $laborCost;
        $this->mdl_products->product_labor_time = $laborHours;
        $this->mdl_products->product_selling_price = $sellingPrice;
        $this->mdl_products->product_percentage_id = $percentageID;


You can use:

$this->modelname =& new modelname(); // skip the & if it is PHP 5
This is guaranteed to create a "blank" model.

It seems to me you have written some 'dangerously' repetitive queries in you foreach($ids as $row) loop.

In this loop I see the follwing queries for every $row:
$result = $this->mdl_products->get();
$result = $this->mdl_product_parts->get();
$this->mdl_products->updateCalc(); is probably also a query.

This works fine if you have 10 or so $id-s. But if you have 10.000 $id-s you will run 30.000 queries in one go. Imagine what that will do to your performance. If there is any way to avoid this I would, if I were you.

Also, you attempt to load two models for every $id. Is there a special reason for this? In most cases you can build your app so a model would only have to be loaded once.

Just a thought, mind you :-)

Hi thank you both for the replies, Yes I did think it was a degrading way to do it but I cant see any other way around it. What I need to do is when a raw material price is changed or when a labor price is changed I need it to update the products total price, now since they are put across separate tables I didn't see another way of dong it, unless you have an idea?


If the total price changes regularly, dependent on the price of labor an raw materials, why do you want to store it in a table at all? Can't you calculate it on-the-fly when you need it, using the current labor an raw material costs?

Thats true, but I think its more likely to stay the same (will only change if the price at buying it in changes) also I am trying to do some stats with the tables so I have a products section on the site and it displays the products and prices. Wouldn't that be more intensive to keep calculating the selling price for each individual product each time someone looks in the products section?

Do you think adding specific functions to the models would make it more streamline? So trying to make it more OO.

Calculating on-the-fly is more expensive, true. But if you leave the calculation to MySQL it's very fast indeed, and even PHP calcualtion will be very fast. Also, you could cache the page.

But it's a trade-off, really. It's just that for scaling purposes this looped query could cause a time-out and thus a corrupted database, with some records having been recalculated, and some not.

I'm not a MySQL guru, but there might also be a way to replace the looped queries with a single caculation query that will update all record at once. Don't ask me how, though...

Thanks JoostV you have been a great help I am just trying to figure out on paper on how to make it more efficient but yes if there is a function that can do that that would be great. I was thinking that maybe joins could come in use but I am not too hot on MySQL either. Thanks again though it definitely needs to be scalable so ill have to think long and hard about it.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.