Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] AR with table names.
#1

[eluser]Clooner[/eluser]
I'm currently rewriting a CI application which has a lot of bad SQL and I'm trying to speed it up significantly. One of the big problems what I run into is that most SQL in this case is not written in AR. I found out why this is because AR messes up large parts of the table names.

It's not uncommon to have a dash in a table name in this project but in a join using AR things get messed up. Lets say we have the table: tax-groups and we are doing a join. AR will actually protect the table name to `tax`-`groups` and not `tax-groups`.

Is this wanted behavior or a bug.?

I know I can simply disable the protect identifiers and put them in myself or get the SQL statement and replace `-` by a - but I would like to use AR.

#2

[eluser]InsiteFX[/eluser]
See the Active Record Select Method!
#3

[eluser]Clooner[/eluser]
[quote author="InsiteFX" date="1329333200"]See the Active Record Select Method!
[/quote]
I don't have the problem with the select statement but rather when using the join statement.

Just checking if this is the wanted behavior or not!

The actual problem is in this preg in the join in the DB_active_rec file.
Code:
// Strip apart the condition and protect the identifiers
if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))

I'll figure out a fix, I'm not strong with regex, Simple questions, how do I submit an improvement to the code?
#4

[eluser]InsiteFX[/eluser]
Active Record escapes all data for protection Automatically.
Except for the set command which you can use TRUE/FALSE to disable it.
#5

[eluser]Clooner[/eluser]
[quote author="InsiteFX" date="1329334595"]Active Record escapes all data for protection Automatically.
Except for the set command which you can use TRUE/FALSE to disable it.
[/quote]
Again, this is also what I want. I want the protection. However the problem with the above regex before it is escaping messes up the joins!

Granted the problem will be solved if I disable the escaping but as you read the pregmatch above it will fail when using table names that have a dash (-) inside it. Therefor the pregmatch needs to be fixed since I'm guessing this is not the wanted behavior!


#6

[eluser]Clooner[/eluser]
This is how the regex should be. Before it would only check \w, which checks if it is a word, however a table name can have underscores, and dashes. Therefore I replaced the \w with [A-Za-z0[9-_.]

Again I'm not really terrific with the regular expressions, there might be a better way but this seems to solve the bug...
Code:
// Strip apart the condition and protect the identifiers
if (preg_match('/([A-Za-z0-9-_.]+)([\W\s]+)(.+)/', $cond, $match))
#7

[eluser]InsiteFX[/eluser]
I think it works with the under score _ in table names is there a reason that you have to use dash?
#8

[eluser]Clooner[/eluser]
[quote author="InsiteFX" date="1329336283"]I think it works with the under score _ in table names is there a reason that you have to use dash?
[/quote]
Yes, I'll have to use a dash since there are other things on top of this application and I cannot go changing the database structure for this. My question is has turned more to, how and where should I file this bug and the fix.
#9

[eluser]Clooner[/eluser]
I filled a report on github

https://github.com/EllisLab/CodeIgniter/issues/1047

Also the easier fix is
Code:
if (preg_match('/([\w\.-]+)([\W\s]+)(.+)/', $cond, $match))




Theme © iAndrew 2016 - Forum software by © MyBB