Welcome Guest, Not a member yet? Register   Sign In
Using database in helpers
#1

[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) {
        
                    /* TAGS */
                    $this->load->database();
                    $sql = "DELETE FROM `tags_table` WHERE `obj_type`=? AND `obj_id`=?";
                    $this->db->query($sql, array($type, $id));
                    
                    $taglist = explode(",", $tags);
                    $tags_this = array();
                    $tags_name = array();
                    foreach ($taglist as $tag) {
                        $tag = trim($tag);
                        $sql = "SELECT * FROM `tags` WHERE `tag_value`=? LIMIT 1";
                        $query = $this->db->query($sql, $tag);
                        
                        if ($query->num_rows() > 0) {
                            $row = $query->row_array();
                            $tags_this[] = $row['tag_id'];
                            $tags_name[$row['tag_id']] = $row['tag_value'];
                        } else {
                            $sql = "INSERT INTO `tags` VALUES (0, ?);";
                            $query = $this->db->query($sql, $tag);
                            $lasttag = $this->db->insert_id();
                            $tags_this[] = $lasttag;
                            $tags_name[$lasttag] = $tag;
                        }
                    }
                    $tagstring = "";
                    foreach ($tags_this as $t) {
                        $sql = "INSERT INTO `tags_table` VALUES (0, ?, ?, ?);";
                        $this->db->query($sql, array($id, $type, $t));
                        $tagstring .= "<a href='/news/tags/".$t."/'>".$tags_name[$t]."</a>, ";
                    }
        return $tagstring;
    }

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
#2

[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.
#3

[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 Smile
#4

[eluser]bretticus[/eluser]
Pretty sure you can call the global get_instance() to get the CI super object:

Code:
$CI =& get_instance();

$CI->load->database();

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.
#5

[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();

$CI->load->database();

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
#6

[eluser]Andrew Ul'din[/eluser]
thank you very much! model is right decision. everything is ok now!

p.s. can close the topic




Theme © iAndrew 2016 - Forum software by © MyBB