gpt4 book ai didi

php - 无法识别 ObjectManager(声明必须与 FixtureInterface->load 兼容)

转载 作者:行者123 更新时间:2023-12-02 16:46:31 25 4
gpt4 key购买 nike

我已经尝试创建 DataFixtures,我认为我的代码是正确的,因为如果我在另一个项目上尝试它是有效的。所以我不明白为什么就在我的实际项目中,对象管理器不起作用,而我的 IDE 是对象管理器的底层。

我的错误:

声明必须兼容 FixtureInterface->load(manager:\Doctrine\Persistence\ObjectManager)

我的代码:

<?php

namespace App\DataFixtures;

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserFixtures extends Fixture
{
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder =$passwordEncoder;
}

public function load(ObjectManager $manager)
{
foreach ($this->getUserData() as [$email,$password,$lastname,$firstname,$company,$language,$enabled,$pictures])
{
$user = new User();
$user->setEmail($email);
$user->setPassword($this->passwordEncoder->encodePassword($user,$password));
$user->setLastname($lastname);
$user->setFirstname($firstname);
$user->setCompany($company);
$user->setLanguage($language);
$user->setEnabled($enabled);
$user->setPictures($pictures);

}
$manager->flush();
}

private function getUserData() : array {
return [
['test@gmail.com','test','paul','marc','WKCompany','BE',1,'https://media.istockphoto.com/photos/businessman-silhouette-as-avatar-or-default-profile-picture-picture-id476085198?k=6&m=476085198&s=612x612&w=0&h=5cDQxXHFzgyz8qYeBQu2gCZq1_TN0z40e_8ayzne0X0=']
];
}
}

最佳答案

看起来很简单的类型错误。我只更改了 ObjectManager 命名空间。

use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserFixtures extends Fixture
{
public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder =$passwordEncoder;
}

public function load(ObjectManager $manager)
{
foreach ($this->getUserData() as [$email,$password,$lastname,$firstname,$company,$language,$enabled,$pictures])
{
$user = new User();
$user->setEmail($email);
$user->setPassword($this->passwordEncoder->encodePassword($user,$password));
$user->setLastname($lastname);
$user->setFirstname($firstname);
$user->setCompany($company);
$user->setLanguage($language);
$user->setEnabled($enabled);
$user->setPictures($pictures);

}
$manager->flush();
}

private function getUserData() : array {
return [
['test@gmail.com','test','paul','marc','WKCompany','BE',1,'https://media.istockphoto.com/photos/businessman-silhouette-as-avatar-or-default-profile-picture-picture-id476085198?k=6&m=476085198&s=612x612&w=0&h=5cDQxXHFzgyz8qYeBQu2gCZq1_TN0z40e_8ayzne0X0=']
];
}
}

关于php - 无法识别 ObjectManager(声明必须与 FixtureInterface->load 兼容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60336159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com