无法删除启用了 FOSOAuthBundle 的用户

Can not delete User with FOSOAuthBundle enabled

我尝试在我的 symfony 3 应用程序中删除一个用户。我使用 FOSOAuthBundleFOSUserBundle.

我试过这段代码:

$em = $this->getDoctrine()->getManager();
$em->remove($user);
$em->flush();

但是我得到这个错误:

An exception occurred while executing 'DELETE FROM user WHERE id = ?' with params [1]:\n\nSQLSTATE[23000]:
Integrity constraint violation: 1451 Cannot delete or update a parent row:
a foreign key constraint fails (`database`.`access_token`, CONSTRAINT `FK_B6A2DD68A76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))

我有这样的配置:

# FOSOAuthServerBundle configuration
fos_oauth_server:
    db_driver: orm
    client_class: OAuthBundle\Entity\Client
    access_token_class: OAuthBundle\Entity\AccessToken
    refresh_token_class: OAuthBundle\Entity\RefreshToken
    auth_code_class: OAuthBundle\Entity\AuthCode
    service:
        user_provider: fos_user.user_provider.username_email
        options:
            access_token_lifetime: 86400
            refresh_token_lifetime: 2.628e+6

如果我获得访问令牌,用户字段始终是 null 尽管在数据库中保存了正确的 ID,并且可以使用此令牌登录。

您需要配置级联操作。您需要决定删除用户时希望在 access_token table 上发生什么。您可以删除de token或将相关用户设置为null。

More info here