首先,我从这里使用 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
最佳答案
您可以在 mysql (
SET sql_mode='ANSI_QUOTES';
) 中打开 ansi_quotes sql 模式,然后在"
中的模式对象名称被正确解释,查看 schema object names for details 上的 mysql 文档。可能有一个 eloquent 级别的设置告诉 eloquent 查询构建器如何引用模式对象名称。更新:将连接对象的语法设置为 mysql,它将使用反引号来引用对象名称。
// set new grammar class DB::connection()->setQueryGrammar(new MySqlGrammar());
关于php - Eloquent ORM 是双引号表和字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34123451/