CodeIgniter Forums
Active Record protect identifier quotes error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Active Record protect identifier quotes error (/thread-74481.html)



Active Record protect identifier quotes error - Steve Galatoczki - 09-30-2019

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


RE: Active Record protect identifier quotes error - fernaog - 09-30-2019

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/general/creating_drivers.html

Regards