使用postPersist($ object)方法更新密码SonataAdminBundle

时间:2016-05-25 15:49:51

标签: symfony fosuserbundle sonata-admin

我需要使用sonata admin int fos userbundle'bcrypt'密码更新密码。

这是我的管理类,

<?php

namespace AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;

class UserAdmin extends Admin {
    public function postPersist($object) {
        $userManager = $this->get('fos_user.user_manager');
        $user = $userManager->findUserBy(array('id'=>$object->id));
        $user->setPlainPassword('test');
        $userManager->updateUser($user);

    }

这是我的用户实体

<?php

// src/AppBundle/Entity/User.php

namespace AdminBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="telephone", type="text", length=30, nullable=false)
     */
    private $telephone;

    /**
     * Set nonotification
     *
     * @param text $telephone
     * @return User
     */
    public function settelephone($telephone) {
        $this->telephone = $telephone;

        return $this;
    }

    /**
     * Get telephone
     *
     * @return text
     */
    public function gettelephone() {
        return $this->telephone;
    }

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="text", length=60, nullable=false)
     */
    private $name;

    /**
     * Set nonotification
     *
     * @param text $name
     * @return User
     */
    public function setname($name) {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return text
     */
    public function getname() {
        return $this->name;
    }

    /**
     * @var string
     *
     * @ORM\Column(name="surname", type="text", length=60, nullable=false)
     */
    private $surname;

    /**
     * Set nonotification
     *
     * @param text $name
     * @return User
     */
    public function setsurname($surname) {
        $this->surname = $surname;

        return $this;
    }

    /**
     * Get surname
     *
     * @return text
     */
    public function getsurname() {
        return $this->surname;
    }

    public function __construct() {
        parent::__construct();
        // your own logic
    }

}

但是我收到了这个错误,

  

尝试调用名为“get”的类的未定义方法   “AdminBundle \管理\ UserAdmin”。你的意思是打电话,例如   “getActiveSubClass”,“getActiveSubclassCode”,“getBaseCodeRoute”,   “getBaseControllerName”,“getBaseRouteName”,“getBaseRoutePattern”,   “getBatchActions”,“getBreadcrumbs”,“getChild”,“getChildren”,   “getClass”,“getClassnameLabel”,“getCode”,“getConfigurationPool”,   “getCurrentChild”,“getCurrentChildAdmin”,“getDataSourceIterator”,   “getDatagrid”,“getDatagridBuilder”,“getExportFields”,   “getExportFormats”,“getExtensions”,“getFilterFieldDescription”,   “getFilterFieldDescriptions”,“getFilterParameters”,“getFilterTheme”,   “getForm”,“getFormBuilder”,“getFormContractor”,   “getFormFieldDescription”,“getFormFieldDescriptions”,   “getFormGroups”,“getFormTabs”,“getFormTheme”,“getIdParameter”,   “getLabel”,“getLabelTranslatorStrategy”,“getList”,“getListBuilder”,   “getListFieldDescription”,“getListFieldDescriptions”,   “getManagerType”,“getMaxPageLinks”,“getMaxPerPage”,   “getMenuFactory”,“getModelManager”,“getNewInstance”,   “getNormalizedIdentifier”,“getObject”,“getObjectIdentifier”,   “getObjectMetadata”,“getParent”,“getParentAssociationMapping”,   “getParentFieldDescription”,“getPerPageOptions”,   “getPermissionsShow”,“getPersistentParameter”,   “getPersistentParameters”,“getRequest”,“getRoot”,“getRootCode”,   “getRouteBuilder”,“getRouteGenerator”,“getRouterIdParameter”,   “getRoutes”,“getSecurityHandler”,“getSecurityInformation”,   “getShow”,“getShowBuilder”,“getShowFieldDescription”,   “getShowFieldDescriptions”,“getShowGroups”,“getShowTabs”,   “getSideMenu”,“getSubClasses”,“getSubject”,“getTemplate”,   “getTemplates”,“getTranslationDomain”,“getTranslationLabel”,   “getTranslator”,“getUniqid”,“getUrlsafeIdentifier”或   “getValidator”?

有人可以调查它会很有帮助。

1 个答案:

答案 0 :(得分:2)

您无法从奏鸣曲管理员类中使用$this->get('service.name')访问容器。你应该使用:

$container = $this->getConfigurationPool()->getContainer();
$userManager = $container->get('fos_user.user_manager');

此外,我不知道您的解决方案是否仍然可行。我会用这样的东西:

public function postPersist($object) {
    $container = $this->getConfigurationPool()->getContainer();
    $entityManager = $container->get('doctrine.orm.entity_manager');
    $object->setPlainPassword('test');
    $entityManager->persist($user);
    $entityManager->flush();
}

自己帮忙并尊重命名惯例(getName而非getname等)