Welcome Guest, Not a member yet? Register   Sign In
Very Weird Database class behavior
#1

[eluser]Eric Cope[/eluser]
When I fetch the result, store it in a local variable, edit the local variable, the variable within the database class is also edited. I don't understand why. Any ideas.

Here is the code
Code:
$this->db->where('companyID', $companyID);
$query = $this->db->get($this->tableName);
$query->result();
var_dump('<pre>',$query);  // var dump #1
$query_result = $query->result();
var_dump('<pre>',$query);  // var dump #2
for($i=0;$i<$query->num_rows();$i++) {
    $query_result[$i]->companyID = (int) $query_result[$i]->companyID;
    $query_result[$i]->userID    = (int) $query_result[$i]->userID;
    $query_result[$i]->branchID  = (int) $query_result[$i]->branchID;
}
var_dump('<pre>',$query);  // var dump #3

Here are the dumps
Code:
object(CI_DB_mysql_result)#17 (7) {
  ["conn_id"]=>
  resource(26) of type (mysql link persistent)
  ["result_id"]=>
  resource(41) of type (mysql result)
  ["result_array"]=>
  array(0) {
  }
  ["result_object"]=>
  array(1) {
    [0]=>
    object(stdClass)#18 (11) {
      ["userID"]=>
      string(2) "48"  // <- unchanged
      ["companyID"]=>
      string(1) "1"   // <- unchanged
      ["branchID"]=>
      string(1) "2"   // <- unchanged
    }
  }
  ["current_row"]=>
  int(0)
  ["num_rows"]=>
  int(1)
  ["row_data"]=>
  NULL
}

object(CI_DB_mysql_result)#17 (7) {
  ["conn_id"]=>
  resource(26) of type (mysql link persistent)
  ["result_id"]=>
  resource(41) of type (mysql result)
  ["result_array"]=>
  array(0) {
  }
  ["result_object"]=>
  array(1) {
    [0]=>
    object(stdClass)#18 (11) {
      ["userID"]=>
      string(2) "48"  // <- unchanged
      ["companyID"]=>
      string(1) "1"   // <- unchanged
      ["branchID"]=>
      string(1) "2"   // <- unchanged
    }
  }
  ["current_row"]=>
  int(0)
  ["num_rows"]=>
  int(1)
  ["row_data"]=>
  NULL
}

object(CI_DB_mysql_result)#17 (7) {
  ["conn_id"]=>
  resource(26) of type (mysql link persistent)
  ["result_id"]=>
  resource(41) of type (mysql result)
  ["result_array"]=>
  array(0) {
  }
  ["result_object"]=>
  array(1) {
    [0]=>
    object(stdClass)#18 (11) {
      ["userID"]=>
      int(48)            // <- changed type
      ["companyID"]=>
      int(1)             // <- changed type
      ["branchID"]=>
      int(2)             // <- changed type
    }
  }
  ["current_row"]=>
  int(0)
  ["num_rows"]=>
  int(1)
  ["row_data"]=>
  NULL
}
#2

[eluser]Colin Williams[/eluser]
In PHP 5, all objects are assigned by reference. You would need to clone() the object to "break" the link between them.
#3

[eluser]Eric Cope[/eluser]
That makes sense now. I am not used to that behavior, but in this case it works well for me.
Thanks!
#4

[eluser]n0xie[/eluser]
[quote author="Eric Cope" date="1262082117"]That makes sense now. I am not used to that behavior, but in this case it works well for me.
Thanks![/quote]
As far as I know, most programming languages pass by reference, except low level (or high level depending on your point of view) languages like C/C++
#5

[eluser]Eric Cope[/eluser]
you nailed it - My favorite language is C. I am used to explicit pass by reference. Was it this way in PHP4?
#6

[eluser]n0xie[/eluser]
Aah that explains the misunderstanding. In PHP4, everything was passed by value, including objects. This has changed in PHP5: all objects are now passed by reference.

A small sidenote if you are used to C/C++, references in PHP are for all intent and purposes just pointers. Although that's not really true: internally it is an alias to the symbol table. This confuses a lot of people coming from C/C++.

If you have time to spare and want some interesting read, I would advice you to take a look at the php manual and especially the comments there (you can read the confusion of some C/C++ programmers between the lines). If you want to know what goes on internally take a look here. Although that was written for PHP4 much of the internal system didn't change much in PHP5, just that the zend engine is 'smarter' now.

Btw. if you want a fresh unaltered copy of your object you can always clone it.
#7

[eluser]Eric Cope[/eluser]
[quote author="n0xie" date="1262129666"]Aah that explains the misunderstanding. In PHP4, everything was passed by value, including objects. This has changed in PHP5: all objects are now passed by reference.

A small sidenote if you are used to C/C++, references in PHP are for all intent and purposes just pointers. Although that's not really true: internally it is an alias to the symbol table. This confuses a lot of people coming from C/C++.

If you have time to spare and want some interesting read, I would advice you to take a look at the php manual and especially the comments there (you can read the confusion of some C/C++ programmers between the lines). If you want to know what goes on internally take a look here. Although that was written for PHP4 much of the internal system didn't change much in PHP5, just that the zend engine is 'smarter' now.

Btw. if you want a fresh unaltered copy of your object you can always clone it.[/quote]

Thank makes more sense too, I learned PHP on PHP4. Thanks for the links!




Theme © iAndrew 2016 - Forum software by © MyBB