简体   繁体   English

Doctrine中的Exception(AbstractPostgreSQLDriver-> convertException)用于在PostgreSQL中插入数据(使用symfony)

[英]Exception(AbstractPostgreSQLDriver->convertException) in Doctrine is for insertion of data in postgresql (Using symfony)

I have a table called user in postgresql. 我在postgresql中有一个名为user的表。 Now when I want to create a record in this table I am getting an exception: 现在,当我想在此表中创建记录时,出现了一个异常:

AbstractPostgreSQLDriver->convertException('An exception occurred while executing \\'INSERT INTO user (user_id, organization_id, client_id, username, password, type, lname, fname, init, email, date, time, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\\' with params [6, 1, 1, "kmr", "kmr12345", "Individual", "c", "a", "b", "abc@gmail.com", "2017-07-30", "16:07:46", "active"]:SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "user"LINE 1: INSERT INTO user (user_id, organization_id, client_id, usern... ^', object(PDOException)) AbstractPostgreSQLDriver-> convertException('执行\\'INSERT INTO用户(user_id,organization_id,client_id,用户名,密码,类型,lname,fname,init,电子邮件,日期,时间,状态)时发生异常值(?,?,? ,?,?,?,?,?,?,?,?,?,?,?)\\'带有参数[6,1,1,“ kmr”,“ kmr12345”,“个人”,“ c”,“ a “,” b“,” abc@gmail.com“,” 2017-07-30“,” 16:07:46“,”活动“]:SQLSTATE [42601]:语法错误:7错误:或以下语法错误在“用户”行附近:INSERT INTO用户(用户ID,组织ID,客户ID,用户ID ... ^',对象(PDOException))

My table schema is : 我的表架构是:

user_id           integer    
organization_id   integer    
client_id         integer    
username          character varying(255)     
password          character varying(255)     
type              character varying(255)     
lname             character varying(255)     
fname             character varying(255)     
init              character varying(255)     
email             character varying(255)     
date              character varying(255)     
time              character varying(255)     
status            character varying(255)

My symfony's parameters.yml is like; 我的symfony的parameters.yml就像;

parameters:
  database_host: localhost
  database_port: 5432
  database_name: task_management
  database_user: postgres
  database_password: 123456
  mailer_transport: smtp
  mailer_host: 127.0.0.1
  mailer_user: null
  mailer_password: null
  secret: ThisTokenIsNotSoSecretChangeIt    

When I am using MySql db (definitely with a different configuration), same PHP code is working. 当我使用MySql db(肯定使用其他配置)时,相同的PHP代码正在运行。

$user = new User();

$user->setUserId(1);
$user->setOrganizationId(1);
$user->setClientId(1);
$user->setUserName('k');
$user->setType('Individual');
$user->setPassword('kmr12345');
$user->setFirstName('a');
$user->setLastName('b');
$user->setInitial('c');
$user->setEmail('abc@gmail.com');
$user->setDate(date('Y-m-d'));
$user->setTime(date('H:m:s'));
$user->setStatus('active');

$em->persist($user);
$em->flush();

return $user->getUserId();

But in case of postgresql I am constantly getting this error. 但是在使用postgresql的情况下,我会不断收到此错误。

Any help will be great for me. 任何帮助对我来说都是很大的。

You don't provide the Doctrine mapping in your Entity, but I think this is where your problem lies. 您没有在实体中提供“学说”映射,但是我认为这就是您的问题所在。 Take a short example of a User-entity: 举一个用户实体的简短示例:

 <?php

 namespace AppBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

 /**
  * @ORM\Entity
  * @ORM\Table(name="user")
  */
 class User
 {
     /**
      * @ORM\Column(type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     private $id;

     /**
      * @ORM\Column(type="username", length=50)
      */
     private $username;

     // ... remaining properties and getters/setters
}

I assume your entity looks something like that. 我认为您的实体看起来像这样。 The problem is the Table-annotation. 问题是表格注释。 you might not even have set a name, but then it will default to your class name, meaning Doctrine will assume something like above. 您甚至可能没有设置名称,但是它将默认使用您的类名称,这意味着Doctrine将采用上述类似的名称。

The problem with this is that user is a reserved word in sql and therefore needs to be escaped. 问题是user是sql中的保留字,因此需要转义。 So it should look something like: 所以它应该看起来像:

@ORM\Table(name="`user`")

It seems MySQL is more forgiving with this than PostgreSQL, but in every case you use a reserved word you should escape it. MySQL似乎比PostgreSQL更宽容,但是在每种情况下,如果您使用保留字,都应该对其进行转义。 This is also mentioned in the Doctrine Documentation Basic Mapping-section 在“ Doctrine文档基本映射”部分中也提到了这一点。

Sometimes it is necessary to quote a column or table name because of reserved word conflicts. 有时由于保留字冲突,必须引用列或表名。 Doctrine does not quote identifiers automatically, because it leads to more problems than it would solve. 教义不会自动引用标识符,因为它导致的问题超出了解决的范围。 Quoting tables and column names needs to be done explicitly using ticks in the definition. 需要使用定义中的对号明确地对表和列名称进行对号。

You can find a list of reserved words in PostgreSQL in it's documentation, in the SQL Keywords-Appendix 您可以在文档的SQL关键字-附录中找到PostgreSQL中保留字的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM