Welcome Guest, Not a member yet? Register   Sign In
[CI 1.6] Active Records : Backticks gone crazy ?
#1

[eluser]Alex007[/eluser]
I just upgraded my CI installation from 1.5.4 to 1.6, and I run into some problems of some of my pages.

The Changelog says:
Quote:All AR queries are backticked if appropriate to the database.

And that seems to be the problem (for my query at least). Here's the interesting code snippet:
Code:
//News
$this->db->select("NewsId");
$this->db->select("Date");
$this->db->select("Title");
$this->db->select("CONCAT(LEFT(Description, 250), CASE WHEN LENGTH(Description) > 250 THEN \"...\" ELSE \"\" END) AS Excerpt");
$this->db->select("LENGTH(Description) AS RealLength");
$this->db->from("News");
$this->db->orderby("Date", "DESC");
$this->db->orderby("NewsId", "DESC");
$this->db->limit(5);
$query = $this->db->get();

It's a pretty simple query, get the 5 latest records from the news table, and truncate them to 250 chars if they exceed that, and add ellipsis for clarity.

But the new backticking thingie really hates that query and fucks it up, so I end up with this error:

Code:
An Error Was Encountered

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN LENGTH(`Description`) > `250` THEN "..." `ELSE` "" `END`) AS Excerpt, LENGT' at line 1

SELECT `NewsId`, `Date`, `Title`, CONCAT(LEFT(`Description`, `250`), `CASE` WHEN LENGTH(`Description`) > `250` THEN "..." `ELSE` "" `END`) AS Excerpt, LENGTH(`Description`) AS RealLength FROM (`News`) ORDER BY `Date` DESC, `NewsId` DESC LIMIT 5

See, the AR backticked a bunch of keywords that shouldn't have been backticked: 250, CASE, ELSE, END...

Now, I know I am streching AR with this particular line...
Code:
$this->db->select("CONCAT(LEFT(Description, 250), CASE WHEN LENGTH(Description) > 250 THEN \"...\" ELSE \"\" END) AS Excerpt");

But how am I supposed to perform that query with AR ? Should I go edit the AR files and add more keyworkds that shouldn't get backticked ?
#2

[eluser]Alex007[/eluser]
Well, I am an idiot and I should re-read the userguid more often:
Quote:$this->Db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

Edit: It works, but it's still weird that AR automatically backticked CASE and ELSE but didn't backtick the words WHEN and THEN...




Theme © iAndrew 2016 - Forum software by © MyBB