Eloquent ORM是双引号表和字段

时间:2015-12-06 22:10:57

标签: php mysql laravel orm eloquent

首先,我在这里使用Eloquent ORM:https://github.com/illuminate/database

如果没有Laravel,我只想在我的项目中使用Eloquent Query Builder。

但是:

use Illuminate\Database\Connection;
use PDO;

$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root');
$connection = new Connection($pdo);

$user = $connection->table('user')->where('name', '=', 'foo')->get();

产生以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"user" where "name" = 'foo'' at line 1

查看在\Illuminate\Database\Connection::select()中执行的查询:

$query变量等于:select * from "user" where "name" = ?

这是一个无效的查询,因为在"周围引用了user

mysql> select * from "user";
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"user"' at line 1

Eloquent引用双引号: https://github.com/illuminate/database/blob/master/Grammar.php#L98

2 个答案:

答案 0 :(得分:4)

  1. 您可以在mysql(<div id="wrapper"> <div class="td1">name</div><div><input type="text"></div> <div class="td1">date of birth</div><div><input type="text"></div> <div class="td1">Shakespeare villain I would like to be</div><div><input type="text"></div> <div class="td1">favo(u)rite do(gh)nut</div><div><input type="text"></div> <div class="td1">pet's blood grou </div><div><input type="text"></div> <div class="td1">mother's maiden name</div><div><input type="text"></div> <div class="td1">favo(u)rite Led Zeppelin track</div><div><input type="text"></div> </div>)中打开ansi_quotes sql模式,然后正确解释SET sql_mode='ANSI_QUOTES';中的模式对象名称,请参阅schema object names for details上的mysql文档。

  2. 可能有一个雄辩的设置,告诉eloquent查询构建器如何引用模式对象名称。更新:将连接对象的语法设置为mysql,它将使用反引号引用对象名称。

    "

答案 1 :(得分:2)

我修复了它:

$connection->setQueryGrammar(new MySqlGrammar());