Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在doctrinMigrations中调用UserPasswordEncoderInterface EncoderPassword_Php_Symfony_Doctrine Migrations - Fatal编程技术网

Php 在doctrinMigrations中调用UserPasswordEncoderInterface EncoderPassword

Php 在doctrinMigrations中调用UserPasswordEncoderInterface EncoderPassword,php,symfony,doctrine-migrations,Php,Symfony,Doctrine Migrations,我正在尝试从Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface使用encodePassword函数对密码进行编码在我的迁移中,这里是我的代码 我尝试了实现UserPasswordEncoderInterface,但没有使用此方法encodePassword 错误是 在null上调用成员函数encodePassword() 私有$encoder为空。因此,您可以对未定义的变量调用函数(在$this->encode

我正在尝试从
Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface使用encodePassword函数对密码进行编码在我的迁移中,这里是我的代码
我尝试了
实现UserPasswordEncoderInterface
,但没有使用此方法encodePassword

错误是

在null上调用成员函数encodePassword()


私有$encoder为空。因此,您可以对未定义的变量调用函数(在
$this->encoder->encodePassword(…);
)。这也会告诉您错误消息。我明白了,那么我如何从postp方法中的最后一个类调用方法encodePassword呢?您缺少一个encoder。您的类没有构造函数。阅读symfony上的依赖项注入和自动连接。迁移类不是自动连接的。请看一个注入外部服务的示例。这需要一些努力,因为它涉及到创建一个新的迁移工厂和各种各样的好东西。一步一步来,很公平。我想如果您的迁移需要创建一个新表并为其种子,那么这是一个有效的要求。
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Ramsey\Uuid\Uuid;
use App\Entity\User;


/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20201112164728 extends AbstractMigration
{
    /**
     * @var UserPasswordEncoderInterface
     */
    private $encoder;


    public function getPassword($password)
    {
        $user = new User();
        return $this->encoder->encodePassword($user,$password);
    }


    public function getDescription(): string
    {
        return '';
    }


    public function up(Schema $schema): void
    {
       
    }

    public function postUp(Schema $schema): void
    {
        $this->connection->insert('users', [
            'id'         => Uuid::uuid4(),
            'email'      => 'xxx@xxx.com',
            'password'   => $this->getPassword('foo'),
   
        ]);
    }

    public function down(Schema $schema): void
    {


    }

}