当尝试在Symfony 4和Doctrine中使用pdo_mysql驱动程序时,我运行$entityManager->flush();
我得到了An exception occurred in driver: could not find driver
输出:
DoctrineDBALExceptionDriverException:
An exception occurred in driver: could not find driver
at vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:125
at DoctrineDBALDriverAbstractMySQLDriver->convertException('An exception occurred in driver: could not find driver', object(PDOException))
(vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:184)
at DoctrineDBALDBALException::wrapException(object(Driver), object(PDOException), 'An exception occurred in driver: could not find driver')
(vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:169)
at DoctrineDBALDBALException::driverException(object(Driver), object(PDOException))
(vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php:47)
.env 文件有
DATABASE_URL=mysql://USER:PASSWORD!@HOSTNAME/DATABASE
学说.yml
doctrine:
dbal:
# configure these for your database server
driver: 'pdo_mysql'
server_version: '5.7.21'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
我已经确保我的docker php-fpm容器具有正确的模块。
root@644cfdc6b45b:/usr/local/etc/php/conf.d# php -m | grep -i pdo
PDO
pdo_mysql
pdo_sqlite
我的 AWS RDS 实例运行的是 MySQL 5.7.21。我也能够创建和运行实体的迁移,因此 bin/console 可以正常工作,但 Symfony 本身没有找到正确的驱动程序。任何有助于使其工作的建议都非常感谢。谢谢!
下面是我的用户控制器
<?php
namespace AppController;
use AppEntityUser;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationResponse;
class UserController extends Controller
{
/**
* @Route("/user", name="user")
*/
public function index()
{
$entityManager = $this->getDoctrine()->getManager();
$user = new User();
$user->setName('Jay');
$user->setUsername('testc');
$user->setEmail('test@test.com');
$user->setPassword(password_hash('testpassword', PASSWORD_DEFAULT));
$entityManager->persist($user);
$entityManager->flush();
return new Response('Saved new user' . $user->getId());
}
}
我必须重新启动我的 docker 容器才能解决此问题。