-
Notifications
You must be signed in to change notification settings - Fork 32
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
Sql::orderBy doesn't quote columns #84
Comments
Actually, the real issue is that the order by should be :
|
To solve this issue now you can use this ugly way: Sql::orderBy('`timestamp` DESC','`id` DESC'); // ORDER BY `timestamp` DESC, `id` DESC I'm seeing to improve this. Something like: Sql::orderBy(array('timestamp' => 'DESC', 'id' => 'DESC')); I'm looking too for the identifier escape. |
Unfortunately, quoting is dbms-specific. Each database has its own quoting model (for example, SQL Sever uses brackets to quote e.g. [column_name]). The Sql class should be dbms independent, but you can extend it... <?php
use Respect\Relational\Sql as RespectSql;
/**
* The SQL dialect with extra statements
*/
class FooBarSql extends RespectSql
{
/**
* Method used to translate from php method calls to SQL instructions.
* It is closely related to __call for the Respect\Relational\Sql class.
*/
protected function build($operation, $parts)
{
switch ($operation) {
case 'orderBy':
return $this->buildParts($parts, '`%s` ');
default:
return parent::build($operation, $parts);
}
}
} ... and then use it on your Mapper or Db instance: <?php
$pdo = new PDO(/* your own PDO config */);
$sqlPrototype = new FooBarSql(); // The class we've extended before
$db = new \Respect\Relational\Db($pdo, $sqlPrototype);
$mapper = new \Respect\Relational\Mapper($db); Both the Mapper and the Db classes will now use the I've used this technique before to make Respect\Relational support |
I've this query which does not quote columns and then mysql retrieve an incorrect order. The table has
timestamp
columnResulting query:
expected:
Is there a way to instruct Sql to quote columns?
The text was updated successfully, but these errors were encountered: