CodeIgniter Forums
Codeigniter AR and JOIN() with brackets? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Codeigniter AR and JOIN() with brackets? (/showthread.php?tid=16678)



Codeigniter AR and JOIN() with brackets? - El Forum - 03-13-2009

[eluser]_jon[/eluser]
I have a bunch of code that used to work in CI 1.6 but since the update, it has ceased to work. It appears that the brackets in this piece of code are being trimmed out:

Code:
$this->db->join('subscriptions', '(subscriptions.subscription_user_id = ' . $this->user->getId() . ' and subscriptions.subscription_thread_id = threads.thread_id)', 'left');

Is there any way I can stop this happening? I don't want to edit the base CI classes if at all possible... it's causing my queries to fail currently!
Thanks.


Codeigniter AR and JOIN() with brackets? - El Forum - 03-13-2009

[eluser]TheFuzzy0ne[/eluser]
Are you using $this->db->from()? If so, ensure $this->db->join() is in the code below it, not above it.

It would probably help more if you could post the entire query part of the function.


Codeigniter AR and JOIN() with brackets? - El Forum - 03-13-2009

[eluser]_jon[/eluser]
I am indeed using $this->db->from(). It is above the joins.

Refer to the current AR at: - here's a snippet from the join() function: http://dev.ellislab.com/svn/CodeIgniter/trunk/system/database/DB_active_rec.php

Code:
// Strip apart the condition and protect the identifiers
if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
{
     $match[1] = $this->_protect_identifiers($match[1]);
     $match[3] = $this->_protect_identifiers($match[3]);
        
     $cond = $match[1].$match[2].$match[3];        
}

Because my condition begins with a bracket, it's not matched by the regular expression, and so is cut out. The CLOSING bracket however, is (due to the .+ part of the regex). Introducing more of my query will probably over complicate things (its tens of lines long)... this -used- to work as the old AR (as far as I know) didn't have this preg_match block in the join function. So:

Code:
(subscriptions.subscription_user_id = ' . $this->user->getId() . ' and subscriptions.subscription_thread_id = threads.thread_id)

becomes

Code:
subscriptions.subscription_user_id = 1 and subscriptions.subscription_thread_id = threads.thread_id)

no good Sad


Codeigniter AR and JOIN() with brackets? - El Forum - 03-13-2009

[eluser]_jon[/eluser]
For now, I've reverted back to CodeIgniter 1.6.3, where the query works perfectly.