Welcome Guest, Not a member yet? Register   Sign In
MySQL Active record IF fuction
#1

[eluser]Cristiano Bianchi[/eluser]
In CI 1.6.1.
When sending a rather complex query using active record, if in a part of the $this->db->select statement there are IF functions, like:

Code:
if(c.typeid = 104, c.custom0, null) AS address,
if(c.typeid = 104, c.custom1, null) AS telephone,
if(c.typeid = 104, c.custom2, null) as email,

erratically, the query is converted to something like:
Code:
if(c.typeid = 104, c.custom0, null) AS address, c.custom1, null) AS telephone, c.custom2, null) as email

The second and third IFs are deleted and melted, resulting in an error. I have turned the auto escaping to false, but that still does not help.

It all works fine in CI 1.5.1.

What is it happening with the query?

Thanks, Cristiano
#2

[eluser]Cristiano Bianchi[/eluser]
This works:

Code:
if(c.typeid = 104, c.custom0, null) AS address,
if(c.typeid = 105, c.custom1, null) AS telephone,
if(c.typeid = 106, c.custom2, null) as email,

Although it is obviously not what I want. Is the DB driver trying to do something too clever?

Thanks, Cristiano
#3

[eluser]Seppo[/eluser]
Since DB cache, the AR explodes by ',' your strings and uses array_unique so you don't ask twice the same field... Try this
Code:
$this->db->select(array(
'if(c.typeid = 104, c.custom0, null) AS address',
'if(c.typeid = 104, c.custom1, null) AS telephone',
'if(c.typeid = 104, c.custom2, null) as email'
), FALSE);
#4

[eluser]Cristiano Bianchi[/eluser]
That's very helpful, thanks!

I must say, though, that splitting the select query by ',' and afterwards merging the way that _merge_cache does it, it is quite wholesome... I should thing this is still a bug.

I think that the FALSE parameter at the end of the select should have an influence on how the split is done...

Thanks again, Cristiano




Theme © iAndrew 2016 - Forum software by © MyBB