![]() |
How can I write this query with Active Records? - 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: How can I write this query with Active Records? (/showthread.php?tid=58209) |
How can I write this query with Active Records? - El Forum - 05-23-2013 [eluser]behnampmdg3[/eluser] Hi; I use this code to send dynamic data to model and run queries: Code: public function results_products($data, $record_start=0,$number_of_records=50) For example: Code: SELECT q2.id, How can I write this query with Active Records? - El Forum - 05-24-2013 [eluser]TheFuzzy0ne[/eluser] Doing that with the Active Record class will probably be impossible, since it's geared more towards simpler queries. When you say you want to change the WHERE condition, do you simply mean the value, or the entire WHERE condition. In either case, query bindings will probably help: http://ellislab.com/codeigniter/user-guide/database/queries.html (bottom of the page). How can I write this query with Active Records? - El Forum - 05-24-2013 [eluser]Pert[/eluser] Wouldn't simple query like this be enough? Code: SELECT * FROM products_table, products_status How can I write this query with Active Records? - El Forum - 05-24-2013 [eluser]behnampmdg3[/eluser] [quote author="TheFuzzy0ne" date="1369386788"]When you say you want to change the WHERE condition, do you simply mean the value, or the entire WHERE condition. In either case, query bindings will probably help: http://ellislab.com/codeigniter/user-guide/database/queries.html (bottom of the page).[/quote]The whole WHERE condition is dynamic. This means I will have to put ligic in the model. ot have multiple queries. Dont really like either. This is where I build the $data. Code: public function validate_search() How can I write this query with Active Records? - El Forum - 05-24-2013 [eluser]behnampmdg3[/eluser] [quote author="pert" date="1369398104"]Wouldn't simple query like this be enough? Code: SELECT * FROM products_table, products_status For smaller tables yes. Once you start dealing with larger data, then no. How can I write this query with Active Records? - El Forum - 05-29-2013 [eluser]Pert[/eluser] [quote author="behnampmdg3" date="1369431473"]Long story short: For smaller tables yes. Once you start dealing with larger data, then no.[/quote] I've started to pre-fetch labels that get used a lot all over the place. Like get all the statuses and assign them into PHP array using ID as array index: Code: $array = array( Then whenever I need to use the text label I can get it with simple <b>$status[$product->status]</b> and you don't need to join tables in your queries. I know joining up few big tables can be hit the memory limit quite easily. How can I write this query with Active Records? - El Forum - 05-29-2013 [eluser]behnampmdg3[/eluser] [quote author="pert" date="1369815168"][quote author="behnampmdg3" date="1369431473"]Long story short: For smaller tables yes. Once you start dealing with larger data, then no.[/quote] I've started to pre-fetch labels that get used a lot all over the place. Like get all the statuses and assign them into PHP array using ID as array index: Code: $array = array( Then whenever I need to use the text label I can get it with simple <b>$status[$product->status]</b> and you don't need to join tables in your queries. I know joining up few big tables can be hit the memory limit quite easily.[/quote]Explain please thanks. How can I write this query with Active Records? - El Forum - 05-29-2013 [eluser]Pert[/eluser] [quote author="behnampmdg3" date="1369826418"]Explain please thanks.[/quote] Sure, as a quite basic example, here goes. First thing I do is get labels for statuses or other items that might be used a lot in the system but all they really do is just give text label to an ID. Code: $tmp = $this->db In your example, you might create additional arrays for classes, etc. Then if I have to get products, I will handle one table in my query: Code: $products = $this->db While you do have to make 2 small quick additional queries in the beginning, you don't have to do any joins for products. |