![]() |
Models Remember Vars? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Models Remember Vars? (/showthread.php?tid=13577) |
Models Remember Vars? - El Forum - 11-27-2008 [eluser]datguru[/eluser] 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 ![]() Thanks Regards Code: //Update All Product Tables so they have new pricing of raw material Models Remember Vars? - El Forum - 11-27-2008 [eluser]m4rw3r[/eluser] You can use: Code: unset($this->modelname); Models Remember Vars? - El Forum - 11-28-2008 [eluser]JoostV[/eluser] 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 :-) Models Remember Vars? - El Forum - 11-28-2008 [eluser]datguru[/eluser] 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? Thanks Models Remember Vars? - El Forum - 11-28-2008 [eluser]JoostV[/eluser] 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? Models Remember Vars? - El Forum - 11-28-2008 [eluser]datguru[/eluser] 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. Models Remember Vars? - El Forum - 11-28-2008 [eluser]JoostV[/eluser] 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... Models Remember Vars? - El Forum - 11-28-2008 [eluser]datguru[/eluser] 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. |