Symfony 3 - 驱动程序发生异常:找不到驱动程序

问题描述:

我只是尝试了 symfony 3,我正在使用 Doctrine ORM 将数据插入到数据库中.当我尝试运行我的查询

I just try symfony 3 and I am on insert data into database using Doctrine ORM. When I try to run my query

$customer = new Customer();
$customer->setAddress('Some Address');
$customer->setName('Customer 1');

$order->setQuantity('100');
$order->setDate(date('Y-m-d'));
$order->setCustomer($customer);

$em = $this->getDoctrine()->getManager();
$em->persist($customer);
$em->persist($order);
$em->flush();

但它返回一个异常:

Uncaught PHP Exception Doctrine\DBAL\Exception\DriverException: "An exception occurred in driver: could not find driver" at /home/hei/Sites/practice/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 121

Parameters.yml:

Parameters.yml:

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: practice
    database_user: root
    database_password: null

配置文件

doctrine:
    dbal:
        driver: pdo_mysql
        host: '%database_host%'
        port: '%database_port%'
        dbname: '%database_name%'
        user: '%database_user%'
        password: '%database_password%'

我还检查扩展是否已通过 php -m 和 phpinfo() 启用和加载.它说 PDO &PDO_Mysql 已启用.

I also check if the extension is enabled and loaded via php -m and phpinfo(). It says that PDO & PDO_Mysql is enabled.

然后在php.ini中加入这两行:

Then add these two lines in php.ini:

extension=pdo.so
extension=pdo_mysql.so

但我仍然遇到相同的 PDO 异常.

but I still got the same PDO Exception.

我的操作系统是 Ubuntu 17.1

My OS is Ubuntu 17.1

我认为您的系统中缺少 php-mysql 包.使用命令安装包

I believe php-mysql package missing in your system. Install the package using the command

sudo apt-get install php-mysql

我希望这能解决您的问题.

I hope this will fix your issue.

我认为您还需要重新启动 Apache 才能解决此问题.安装php-mysql后使用以下命令重启Apache

I think you need to restart Apache also in order to fix the issue. After installing php-mysql use the following command to restart Apache

 sudo service apache2 restart.