How to override FOSUserBundle/Doctrine/UserManager.php

社会主义新天地 提交于 2019-12-10 22:19:59

问题


I am integrating fileupload system with fosuserbundle

I need to override updateUser function in

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

I copied this file to

/ACME/UserBundle/Doctrine/UserManager.php

But it doesn't work.

This is my temporary /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();
    }
}     

回答1:


You can not override the UserManager by simple bundle inheritance as you tried because it is used as a service throughout the FOSUserBundle.

Create a service configuration in your bundle ( i.e. services.xml ) and add your own acme.user_manager service:

<?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>

Then configure FOSUserBundle to use this service as the UserManager in your config.yml

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



回答2:


Just resolved the similar problem. Maybe will be helpful for someone. 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%"]


来源:https://stackoverflow.com/questions/16731279/how-to-override-fosuserbundle-doctrine-usermanager-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!