Welcome Guest, Not a member yet? Register   Sign In
Active Record protect identifier quotes error
#1

Hi,

I use the $this->db->select second parameters, to disable protect in my subquery, 
so the subquery string would not contain quotes. Ex:

Code:
$this->db->select('col1, col2, col3, col4', FALSE);
...
...
$subquery = $this->db->get_compiled_select();


But if I put my subquery into second select's 'from' function (Ex: $this->db->from('('.$subquery.')' AS Foo) ),
my query string will contain quotes. 

I found in the from function of the query builder class, a $this->protect_identifiers function with fix third parameters (NULL).
If I change the original NULL value to FALSE, the query will works properly.

My solution: 
Code:
public function from($from, $protected = NULL)
{
  foreach ((array) $from as $val)
  {
    if (strpos($val, ',') !== FALSE)
    {
      foreach (explode(',', $val) as $v)
      {
        $v = trim($v);
        $this->_track_aliases($v);
        $this->qb_from[] = $v = $this->protect_identifiers($v, TRUE, $protected, FALSE);
        if ($this->qb_caching === TRUE)
        {
          $this->qb_cache_from[] = $v;
          $this->qb_cache_exists[] = 'from';
        }
      }
    }
    else
    {
      $val = trim($val);
      $this->_track_aliases($val);
      $this->qb_from[] = $val = $this->protect_identifiers($val, TRUE, $protected, FALSE);
      if ($this->qb_caching === TRUE)
      {
        $this->qb_cache_from[] = $val;
        $this->qb_cache_exists[] = 'from';
      }
    }
  }
  return $this;
}

My question: Is there another way to fix the error, without modifying the core code?

Thanks, Steve
Reply
#2

Hi,

You could create a driver and extend the CI_DB_query_builder class. In this way you can modify the from function to your liking without modifying the core.

https://codeigniter.com/user_guide/gener...ivers.html

Regards
Reply




Theme © iAndrew 2016 - Forum software by © MyBB