Skip to content

Instantly share code, notes, and snippets.

@AkenRoberts
Last active April 22, 2016 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkenRoberts/4371198 to your computer and use it in GitHub Desktop.
Save AkenRoberts/4371198 to your computer and use it in GitHub Desktop.
Idea to help solve CodeIgniter Query Builder issue with SELECT strings being exploded at commas.
/**
* Select
*
* Generates the SELECT portion of the query
*
* @param string
* @param mixed
* @return CI_DB_query_builder
*/
public function select($select = '*', $escape = NULL)
{
if (is_string($select))
{
// Find all parentheses sets.
preg_match_all('/\((.*)\)/', $select, $func_groups);
// Inside each parentheses set, replace all commas with
// a temporary value, which will be reverted back later.
// We do this to avoid issues with using explode() later.
foreach ($func_groups as $func_group)
{
$func_replace = str_replace(',', '{comma}', $func_group);
$select = str_replace($func_group, $func_replace, $select);
}
$select = explode(',', $select);
}
// If the escape value was not set will will base it on the global setting
is_bool($escape) OR $escape = $this->_protect_identifiers;
foreach ($select as $val)
{
// Put commas back in where they were temporarily replaced.
$val = str_replace('{comma}', ',', trim($val));
if ($val !== '')
{
$this->qb_select[] = $val;
$this->qb_no_escape[] = $escape;
if ($this->qb_caching === TRUE)
{
$this->qb_cache_select[] = $val;
$this->qb_cache_exists[] = 'select';
$this->qb_cache_no_escape[] = $escape;
}
}
}
return $this;
}
@papacarp
Copy link

This worked great for me. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment