php - Eloquent ORM 是双引号表和字段

标签 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

最佳答案

  1. 您可以在 mysql (SET sql_mode='ANSI_QUOTES';) 中打开 ansi_quotes sql 模式,然后在 " 中的模式对象名称被正确解释,查看 schema object names for details 上的 mysql 文档。

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

    // set new grammar class
    
    DB::connection()->setQueryGrammar(new MySqlGrammar());
    

关于php - Eloquent ORM 是双引号表和字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34123451/

相关文章:

mysql - 与同表的增量数据进行Join

使用枚举列进行 Laravel 验证

php - Laravel 4 动态路由 - 过度杀伤力?

php - 清除cookies,为什么需要特定值?

php - 如何在SQL数据库中同时保存中英文字符

javascript - 使用javascript for循环,访问php数组变量

java - 如何针对这种情况创建 hibernate 映射?

MySQL存储过程循环超时

jquery - Laravel,jQuery 文件上传错误 - "Call to a member function store() on array"

php - 在 CakePHP 中插入计算机生成的输出