Welcome Guest, Not a member yet? Register   Sign In
Resetting the value of $this
#1

[eluser]nydeveloper[/eluser]
Is there a way to reset the active value of $this to null or empty?

The following statement outputs variable names and values that I used in a previous query, and I need them cleared out before I perform my next query (on a different table):

Code:
var_dump($this);

What I'd like is for this var_dump to return an empty value.

Thanks in advance.
#2

[eluser]Aken[/eluser]
Lol. You might run into one or two problems by unsetting $this...

If you're using the DB's active record, try
Code:
$this->db->reset_query()
#3

[eluser]nydeveloper[/eluser]
For some reason this causes the code to stop running. No errors, no warnings, etc. However, the code stops running completely once it hits this line. Nothing below it is executed.

The same goes for the following lines that I've tried:
Code:
$this->db->_reset_write();
$this->db->free_result();
$this->db->_reset_select();

#4

[eluser]InsiteFX[/eluser]
Try:
Code:
$this->db->close();

// then reopen the database.
$this->load->database();
#5

[eluser]nydeveloper[/eluser]
Try:

Code:
$this->db->close();

// then reopen the database.
$this->load->database();

I tried this, and it seemed to have no effect. I may have explained the issue incorrectly. The var_dump($this); statement returns the same output both before and after the close and load statements above. Here is a sample output:

Quote:object(Student_Model)#14 (21) { ["student_id"]=> string(1) "6" ["student_sector"]=> int(90) ["student_grouping"]=> int(40) ["grade_average"]=> int(3) }

I've removed a few of the variables from the output, but you get the point... I don't want any of these particular variables saved in memory. The reason for this is I'm trying to update a different table, one without these particular variables. However, when I try updating one specific column, the above variables are automatically added to the update statement. Sad
#6

[eluser]Aken[/eluser]
Okay, we have a much different idea of what $this is than you do, apparently.

Post your model code and what the end result you're trying to accomplish is.
#7

[eluser]nydeveloper[/eluser]
Code:
function set_student_averages($student_id, $grade_average)
{

var_dump($this);
$this->db->close();
// then reopen the database.
        $this->load->database();
var_dump($this);
$this->db->where('student_id', $student_id);
$this->db->set('grade_average', $grade_average);
$this->db->update('student_misc', $this)
}

The goal of this function is to update the 'grade_average' column for a specific 'student_id' in the 'student_misc' table. This function (set_student_averages) is being called by another function in the same model, with the following two variables being passed from said function: 'student_id' and 'grade_average'

The update statement above, for some reason, is adding all of the variables listed in my 'object' output above in the thread, including 'student_sector' and 'student_grouping'. These variables *do not* exist in the student_misc table. They actually exist in another table that was used in a previous query (an insert).

My goal is to completely clear them out of memory (not sure why they are being stored - maybe in Active Record?), so the SQL generated by the above update statement only includes the 'student_id' and 'grade_average' variables. Hope this clears things up a bit, and thank you for your help so far!
#8

[eluser]InsiteFX[/eluser]
Code:
$this refer's to the current object

This sounds like a coding problem because we use active record all the time without a problem like this!

We need to see your code to see what is going on...
#9

[eluser]Aken[/eluser]
Why are you using $this as a second parameter of the update() method? That's where your problem is coming from - you don't need that. The second parameter is for data (aka the columns) that you want to update. If you're using set(), you don't need to supply data also.

You could make this simple update using the update() method all by itself. Check the docs.
#10

[eluser]nydeveloper[/eluser]
Quote:Why are you using $this as a second parameter of the update() method? That’s where your problem is coming from - you don’t need that. The second parameter is for data (aka the columns) that you want to update. If you’re using set(), you don’t need to supply data also.

That was the problem. I based my code off of a bad example initially... big mistake obviously. Thank you so much!!!




Theme © iAndrew 2016 - Forum software by © MyBB