dongxie8856 2017-07-30 05:26
浏览 69
已采纳

Doctrine中的异常(AbstractPostgreSQLDriver-> convertException)用于在postgresql中插入数据(使用symfony)

I have a table called user in postgresql. 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))

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;

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.

$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.

Any help will be great for me.

  • 写回答

1条回答 默认 最新

  • douyiken0968 2017-07-30 13:40
    关注

    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.

    The problem with this is that user is a reserved word in sql and therefore needs to be escaped. 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. This is also mentioned in the Doctrine Documentation Basic Mapping-section

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸