I have a bit confusing problem. I've created some class where user can create an instance of it and set some parameters via exposed public functions. In the constructor of that class I create instance of DB connection :
class MyClass{
public function __construct()
{
$this->db_obj = \DB::select('*')->from('table');
}
}
with some basic query and by these public functions, developer may modify params of that basic query.
So it looks like this:
$obj = new Class();
$obj->some_option('aaa', 'bbb');
$obj->some_other_option( 17 );
and it the class:
function some_option($x, $y){
$this->db_obj->where( 'x','=', $y );
}
Now, this class also has get data method and this simply executes the query.
The problem is that I have to iterate through some collection of the objects and inject this object (with different params) inside of the iterated object. Sometimes I need to clone that object before injecting it, to modyfy the params but it looks like for some reason all the params passed to the DB object are piling up, even if I clone my object !
Something tells me that the framework uses static variable to use to created instance so even if I clone the object, DB query is global across the objects.
Is there something I can do to avoid it ?
The only solution I have is to create DB connection just before executing it and store all the options inside the object, instead building up the query in the runtime but it was very handy to do it that way.
There is a global database instance for every database connection. You can compare it to a raw PDO instance, which also can't be shared this way.
You could clone database instances, but if you do this a lot, you'll quickly run out of memory, because you'll clone everything that is attached to it.
It is more logical to use model objects (Model_Crud or ORM) to interact with the DB.