• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active Record Conflict

#1
Hello
Are there any way to prevent Active record query builder conflicts. Imagine, In my controller, I am using Model "A" that uses Active record query builder. Between this, i want to use a library that it also uses Active record query builder to make it's query, But the problem is here, what i will have in the final, is a mixture of both of them. what should i do to solve that?
Reply

#2
Maybe this can help you
http://www.codeigniter.com/user_guide/da...ry-builder
Reply

#3
Don't create this "mixture". What you have in model A shouldn't affect model B.
Reply

#4
(09-30-2015, 01:41 PM)Anisi Wrote: Hello
Are there any way to prevent Active record query builder conflicts. Imagine, In my controller, I am using Model "A" that uses Active record query builder. Between this, i want to use a library that it also uses Active record query builder to make it's query, But the problem is here, what i will have in the final, is a mixture of both of them. what should i do to solve that?

This is because the Query Builder module extends the DB Driver which is typically a single instance for each DB connection.

I have hacked a new library QB that sits on top of the DB driver.  I'd like to see this make it in to CI officially someday, but not sure
my solution is correct.

Create a new library (say Query_builder.php) by copying the query_builder (or active_record in 2.x) code from CI system/database to your application library.  Change the class definition to:
Code:
class Query_builder {

and add the following after the instance variable declarations and before the functions are declared:
Code:
 protected $DB;

 // Use Dependency Injection here to init DB variable.
 public function __construct(&$db = null) {
     if ($db) {
         $this->DB = $db;
     } else {  
         $CI =& get_instance();
         $this->DB = $CI->db;
     }
 }

 public function __call($name, $arguments) {
     $r = call_user_func_array(array($this->DB, $name), $arguments);
     return $r;
 }

 public function __get($name) {
     return $this->DB->{$name};
 }

This makes the query buillder object behave like a $CI->db by forwarding any un-handled functions to the CI db instance using the magic __get and call_user_func_array().  Each query builder instance has independent state variables and there are no more conflicts.  Back in the 2.x days there was a version of active_record that was posted that did this internally, but I think my solution is more elegant with fewer code changes.

Under 2.x you have to copy and modify a _delete() function from the driver, but not necessary in 3.0.

To use it:
Code:
   // db should be already loaded, but just in case...
   isset($CI->db) AND is_object($CI->db) OR $CI->load->database(); // CI 3.0 init code

   $this->load->library('Query_builder'); 
   $qb = new Query_builder();
   // $qb behaves exactly like $this->db;
   $query = $qb->select('field')->where('id',$id)->get('table');
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.