-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Table name can not use the alias #831
Comments
yes, $this->qb_aliased_tables should be renamed to $this->QBAliasedTables of BaseConnection.php in protectIdentifiers method line no 1028. |
How can I repair it? |
Doh! That's totally my fault, from when I refactored to allow unique instances of the query builder. I didn't even think of that. Since it's a holiday this week in the United States, I won't have any time to look into this until next week, but will dig in early next week, if no one else has submitted a PR to fix it just yet. If you don't hear from me by Wednesday next week be sure to ping me, since I've also got a paying side project eating up my available time right now. |
@lonnieezell as you said in above comment, it's just a reminder on this issue. |
Looking at the tests that feature is tested and should be working fine. It looks like you're setting the table alias incorrectly - there shouldn't be any use of |
yes agree, but people use AS in their select or join query. |
And that would be correct sql for select or join but not for table aliases.
…Sent from my iPhone
On Dec 2, 2017, at 6:36 AM, rrajesh011 ***@***.***> wrote:
yes agree, but people use AS in their select or join query.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@lonnieezell
but error
it doesn't work I modified BaseBuilder.php and BaseConnection.php like this: and it's ok, But it's not a good way to do it, but it can be solved. I think you should have a better solution |
I was just looking at our tests and realized we don’t test aliases with live databases only against expected strings so I will need to check into that. What database are you using? |
I using mysql |
Perhaps we can determine whether there is table alias to add table prefix processing. //BaseBuilder.php The protectIdentifiers method adds the table prefix
public function join($table, $cond, $type = '', $escape = null)
{
......//Omit some code
$cond = ' ON ';
for ($i = 0, $c = count($conditions); $i < $c; $i ++ )
{
$operator = $this->getOperator($conditions[$i]);
$cond .= $joints[$i];
$cond .= preg_match("/(\(*)?([\[\]\w\.'-]+)" . preg_quote($operator) . "(.*)/i", $conditions[$i], $match) ? $match[1] . $this->db->protectIdentifiers($match[2]) . $operator . $this->db->protectIdentifiers($match[3]) : $conditions[$i];
}
......//Omit some code
} //BaseConnection.php
public function protectIdentifiers($item, $prefixSingle = false, $protectIdentifiers = null, $fieldExists = true)
{
......//Omit some code
// Break the string apart if it contains periods, then insert the table prefix
// in the correct location, assuming the period doesn't indicate that we're dealing
// with an alias. While we're at it, we will escape the components
if (strpos($item, '.') !== false)
{
$parts = explode('.', $item);
......//Omit some code
// Is there a table prefix defined in the config file? If not, no need to do anything
if ($this->DBPrefix !== '')
{
// We now add the table prefix based on some logic.
// Do we have 4 segments (hostname.database.table.column)?
// If so, we add the table prefix to the column name in the 3rd segment.
if (isset($parts[3]))
{
$i = 2;
}
// Do we have 3 segments (database.table.column)?
// If so, we add the table prefix to the column name in 2nd position
elseif (isset($parts[2]))
{
$i = 1;
}
// Do we have 2 segments (table.column)?
// If so, we add the table prefix to the column name in 1st segment
else
{
$i = 0;
}
// This flag is set when the supplied $item does not contain a field name.
// This can happen when this function is being called from a JOIN.
if ($fieldExists === false)
{
$i ++;
}
// Verify table prefix and replace if necessary
if ($this->swapPre !== '' && strpos($parts[$i], $this->swapPre) === 0)
{
$parts[$i] = preg_replace('/^' . $this->swapPre . '(\S+?)/', $this->DBPrefix . '\\1', $parts[$i]);
}
// We only add the table prefix if it does not already exist
elseif (strpos($parts[$i], $this->DBPrefix) !== 0)
{
// !!! important //
//!!! add table prefix//
$parts[$i] = $this->DBPrefix . $parts[$i];
}
// Put the parts back together
$item = implode('.', $parts);
}
......//Omit some code
return $item . $alias;
} |
I think that only the $this->qb_aliased_tables in BaseConnection.php is needed to get the value |
I modified BaseBuilder.php and BaseConnection.php like this: http://wx4.sinaimg.cn/large/82c9caaegy1fnfzb1ghsjj211u125wl2.jpg Is this the best solution? |
@jinmarcus Yup. You basically had it. I did it slightly differently, but your fix was right on. Thanks. |
Table name can not use the alias
Wrong
print sql :
"hero_" is DBPrefix
I find system\Database\BaseConnection.php
in 1089 line
$this->qb_aliased_tables Should it be $this->QBAliasedTables ?
but I don't know how to go to fixed
The text was updated successfully, but these errors were encountered: