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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误