如何覆盖FOSUserBundle / Doctrine / UserManager.php

时间:2013-05-24 09:05:23

标签: symfony fosuserbundle

我正在将fileupload系统与fosuserbundle

集成

我需要覆盖updateUser函数  在

/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Doctrine/UserManager.php

我将此文件复制到

/ACME/UserBundle/Doctrine/UserManager.php

但它不起作用。

这是我临时的/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Doctrine/UserManager.php

public function updateUser(UserInterface $user, $andFlush = true)
{
    $this->updateCanonicalFields($user);
    $this->updatePassword($user);

   //it works but it  should not be used here.
    $user->upload();
    //

    $this->objectManager->persist($user);
    if ($andFlush) {
        $this->objectManager->flush();
    }
}     

2 个答案:

答案 0 :(得分:5)

您无法通过简单的捆绑继承覆盖UserManager,因为它在整个FOSUserBundle中用作服务。

在您的包中创建服务配置(即services.xml)并添加您自己的acme.user_manager服务:

<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>

    <parameters>
        <parameter key="acme.user_manager.class">ACME/UserBundle/Doctrine/UserManager</parameter>
    </parameters>

    <services>
        <service id="acme.user_manager" class="%acme.user_manager.class%" public="false">
            <argument type="service" id="security.encoder_factory" />
            <argument type="service" id="fos_user.util.username_canonicalizer" />
            <argument type="service" id="fos_user.util.email_canonicalizer" />
            <argument type="service" id="fos_user.entity_manager" />
            <argument>%fos_user.model.user.class%</argument>
        </service>
    </services>
</container>

然后配置FOSUserBundle以将此服务用作config.yml中的UserManager

fos_user:
    # ...
    service:
        user_manager: acme.user_manager

答案 1 :(得分:0)

刚刚解决了类似的问题。也许对某人有帮助。 Symfony 3.3.6,FosUserBundle 2.0.1

services:
    you_bundle.user_manager:
        class: YourBundle\Services\UserManager
        arguments: ["@fos_user.util.password_updater", "@fos_user.util.canonical_fields_updater", "@doctrine.orm.entity_manager", "%fos_user.model.user.class%"]