Using database in helpers - 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: Using database in helpers (/showthread.php?tid=26699) |
Using database in helpers - El Forum - 01-21-2010 [eluser]Andrew Ul'din[/eluser] In my project I use tags for news. I want to write data to database and get tag-string. So i want do it by helper. This is the code of helper: Code: function showmetags($id, $type, $tags) { code in controller: Code: $tagstring = showmetags($id, 'news', $this->input->post('tags')); but all crash on this string in hepler: Code: $this->db->query($sql, array($type, $id)); Can't do query to database. Why? How i can do it from helper? If code write in controller - all work! From helper - no. p.s. sorry for my english Using database in helpers - El Forum - 01-21-2010 [eluser]John Pantoja[/eluser] I think helpers are disconnected from CI's super object (if I'm using the term correctly)... so you can't do it. Using database in helpers - El Forum - 01-21-2010 [eluser]John Pantoja[/eluser] and they are procedural so they have no concept of $this (OOP) so scratch my first concept if it's not correct and this is a better answer Using database in helpers - El Forum - 01-21-2010 [eluser]bretticus[/eluser] Pretty sure you can call the global get_instance() to get the CI super object: Code: $CI =& get_instance(); However, helpers were really not meant to do database work. Why not turn your function into a model method? It's pretty simple calling it (albeit a little longer to type: not a big deal) Code: $tagstring = $this->tags->showmetags($id, 'news', $this->input->post('tags')); Not a huge difference really. Using database in helpers - El Forum - 01-21-2010 [eluser]John Pantoja[/eluser] [quote author="bretticus" date="1264080909"]Pretty sure you can call the global get_instance() to get the CI super object: Code: $CI =& get_instance(); However, helpers were really not meant to do database work. Why not turn your function into a model method? It's pretty simple calling it (albeit a little longer to type: not a big deal) Code: $tagstring = $this->tags->showmetags($id, 'news', $this->input->post('tags')); Not a huge difference really.[/quote] Ah ha, that's something I forgot twice already (calling get_instance()). ^-best answer Using database in helpers - El Forum - 01-21-2010 [eluser]Andrew Ul'din[/eluser] thank you very much! model is right decision. everything is ok now! p.s. can close the topic |