Using Codeigniter (v1.7.2) Active Record Class to query a MSSQL Database (external DB, I don’t control table or field names)
The following Code:
$this->db->select('[Key Field], MemberInfo.OtherField');
$this->db->limit(10);
$this->db->from('primaryTable');
$this->db->join('MemberInfo', '[Member Number] = Member_Number', 'left');
$this->db->where('[Member Number] = 573');
$this->db->where('[Incident Date] BETWEEN '. $DateStart . ' AND ' . $DateEnd);
Produces the following Query (which has a syntax error)
SELECT TOP 10 [Key Field], MemberInfo.OtherField
FROM primaryTable
LEFT JOIN MemberInfo ON Member Number] = Member_Number
WHERE [Member Number] = '573'
AND [Incident Date] BETWEEN 2012-01-01 AND 2012-07-19
Note the JOIN clause, which is missing the opening ‘[‘ on the field name
I tried using double quotes, which yielded the same result.
Anyone have ideas, is this a known bug in 1.7.2 (which I know is old, working up updating)?
UPDATE
issue is present in 2.1.2 also
Accepted answer below is also the fix for 2.1.2: change the regex check in the JOIN function to look for the opening [
The problem is in the parameter
ON. There does not exist an integrated solution in the framework (not in version 2).You have 2 options:
Altering the core of the library active_record
Write the query in a complete chain with concatenations using $this->db->query();