CodeIgniter Forums
DB Where function doesn't resets after running query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: DB Where function doesn't resets after running query (/thread-1973.html)



DB Where function doesn't resets after running query - El Forum - 07-09-2007

[eluser]Benjamin David[/eluser]
Hi !

I'm having a weird bug with the active record class. It's as if it remembers the where conditions I had made in previous queries. Here it is :

Here is the first function, it's a recursive one :
Code:
function delete_media_entry($id) {
            if($id == 0) {
                return FALSE;
            }
            
            $rip_medias = array();
            $errors = 0;
            $delete_root_media = new Media_model;
            $rip_medias[$id] = $delete_root_media->get_media($id);
            echo "get ($id)<br />";
            echo $this->db->last_query();
            /*if(!is_object($rip_medias[$id])) {
                var_dump($rip_medias[$id]);
                echo "<br />".$id;
            }*/
            if($rip_medias[$id]->file_type == 'folder') {
                $medias = new Media_model;
                $medias = $medias->get_medias($id);
                foreach($medias as $media) {
                    if(!is_object($media)) {
                        //var_dump();
                    }
                    if($media->file_type == 'folder') {
        
                        /* recursion */
                        echo "<br />----recursion----<br />";
                        $this->delete_media_entry($media->id);
                        //echo "dossier: ".$media->name."(".$media->id.")<br />";
                    } else {
                        //echo "fichier:".$media->name."(".$media->id.")<br />";
                        $rip_medias[$media->id] = $media;
                    }
                }
            }

            
            /* suppression des entrees */
            
            foreach($rip_medias as $rip_media) {
                $delete_media = new Media_model;                
                $delete_result = $delete_media->_delete_media_row($id);

                if($delete_result == FALSE) {
                    $errors++;
                }
            }

            if($errors == 0) {
                return TRUE;
            }
            
            return FALSE;

        }

Here is the second one that gets the media:

Code:
function get_media($id)
    {
            $this->db->where(array('id' => $id));
            
            $query = $this->db->get($this->tables['media']);
            
            if($query->num_rows() == 1) {
                $media_row = $query->row();
                $media_object = $this->set_db_object($media_row);
            
                //echo $this->db->last_query()."<br />";
                return $media_object;
            }
            //echo $this->db->last_query()."<br />";
            return false;
    }

I have some data in the table but the errors I'm getting are weird. In that case, I only ask for one row, I give the id and I'm awaiting a single row answer. But at some point, there is something that makes the class remember my previous where condition, but can't get to handle where... I'm wondering why it doesn't the db where function doesn't seem to reset after each query has run.

By the way, here's a copy of the errors I'm getting. Thanks for helping !
Code:
get (2)
SELECT * FROM arc_media WHERE id = '2'
----recursion----
get (5)
SELECT * FROM arc_media WHERE id = '5'
----recursion----
get (6)
SELECT * FROM arc_media WHERE id = '6'
----recursion----
get (9)
SELECT * FROM arc_media WHERE id = '6' AND id = '6' AND id = '5' AND id = '5' AND id = '9' LIMIT 1
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/media_model.php

Line Number: 63

----recursion----
get (10)
SELECT * FROM arc_media WHERE id = '9' AND id = '10' LIMIT 1
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/media_model.php

Line Number: 63

----recursion----
get (11)
SELECT * FROM arc_media WHERE id = '10' AND id = '11' LIMIT 1
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: models/media_model.php

Line Number: 63



DB Where function doesn't resets after running query - El Forum - 07-10-2007

[eluser]OwanH[/eluser]
CI Ben, have you configured CI to cache your database queries? If you have be sure to clear the cache each time the database is updated by an insert/update/delete operation.


DB Where function doesn't resets after running query - El Forum - 07-10-2007

[eluser]marcalj[/eluser]
After every finish SELECT make a free_result of a query.

Code:
$query->free_result();

I hope it helps.

Salut!


DB Where function doesn't resets after running query - El Forum - 07-10-2007

[eluser]Benjamin David[/eluser]
Hi and thanks for answering !

OwanH, I checked and didn't see anything activated. I have never activated it actually.

marcalj, thanks that's exactly what I was looking for but it unfortunately, it doesn't work.

I hope I'll get some other ideas, otherwise I'll have to use a normal SQL request instead of active records...


DB Where function doesn't resets after running query - El Forum - 07-10-2007

[eluser]OwanH[/eluser]
CI Ben, could you post the code for your Media_model::get_media method? I'd like to take a look at it to see if there could anything in the method that could be causing the problem. From the errors you have shown, I'm assuming that the SELECT query generated by this method is not right.


DB Where function doesn't resets after running query - El Forum - 07-11-2007

[eluser]Benjamin David[/eluser]
Hi ! First of all, thanks for taking the time to take a look at this. Here is my get_media function :

Code:
function get_media($id)
    {
            //$this->db->where(array('id' => $id));
            $this->db->where('id', $id);
            $query = $this->db->get($this->tables['media']);

            
            if($query->num_rows() == 1) {
                $media_row = $query->row();
                $media_object = $this->set_db_object($media_row);
            
                return $media_object;
            }

            return false;
    }

I also give you the set_db_object function. It should be useless, but just in case...

Code:
function set_db_object($media_row) {
        $media_object = new Media_model;
        foreach($media_row as $key => $value) {
            $media_object->$key = $value;
        }            
        
        return $media_object;
    }



DB Where function doesn't resets after running query - El Forum - 07-11-2007

[eluser]OwanH[/eluser]
OK this may seem like a crazy suggestion but try replacing but try replacing these two lines:

Code:
$this->db->where('id', $id);
$query = $this->db->get($this->tables['media']);

with this line:

Code:
$query = $this->db->getwhere($this->tables['media'], array('id' => $id));

To be honest I actually don't see anything wrong with your code, and I don't think you should be getting the problem you are getting, especially since you have not enabled CI's query caching, but hey you could give this a try and see if it helps Smile.