I've got a SelectQuery which I've created using db_select(). Then, after I've executed the query, I'm trying to clone it as I need to generate a few more queries based on the original one.
However, for some reason the clones I get from the original query doesn't seem to be clones. This is what I do.
// Create query
$q0 = db_select('track', 't');
$q0->fields('t', array('id','number','title');
[...]
$tracks = $q0->execute()->fetchAll();
// Clear fields on original query.
$fields = &$q0->getFields();
$fields = array();
// Clone original query. The below queries should not have
// any fields set as they just got cleared above.
$q1 = clone $q0;
$q2 = clone $q0;
// This gets weird. The queries $q1 and $q2 should be separate
// clones, independent of each other. So should their fields.
// However, setting fields on $q1 also sets fields on $q2.
$q1->fields('t', array('id'));
// Output : Array ( [id] => Array ( [field] => id [table] => t [alias] => id ) )
print_r($q2);
The fields() function on SelectQuery simply sets an array (see below). I thought that cloning an instance of a class containing an array would copy those arrays by value...and not by reference? Is this a special cloning "feature"?
protected $fields = array();
public function &getFields() {
return $this->fields;
}