无法将 PHP 值转换为类型日期。应为以下类型之一:null、DateTime

Could not convert PHP value to type date. Expected one of the following types: null, DateTime

我用 fakerphp 创建了 fixtures 来生成假的出生日期但无法加载 fixtures 文件并收到此错误消息:无法将 PHP 值 '1970-01-01' 转换为输入日期。应为以下类型之一:null、DateTime。你知道我应该修改什么吗?

这是我的 php 实体文件代码的一部分:


/**
 * @ORM\Column(type="date")
 */
private $birthDate;


public function getId(): ?int
{
    return $this->id;
}

public function getEmailAddress(): ?string
{
    return $this->emailAddress;
}

public function setEmailAddress(string $emailAddress): self
{
    $this->emailAddress = $emailAddress;

    return $this;
}

public function getBirthDate(): ?string
{
    return $this->birthDate;
}

public function setBirthDate(string $birthDate): self
{
    $this->birthDate = $birthDate;

    return $this;
}

还有我的灯具:

public function load(ObjectManager $manager):void
{
    $faker = Factory::create(('fr_FR'));
    $faker->seed(1234);

    for ($i = 0; $i <= 20; $i++) {
        $member = new Member();

        $member->setEmailAddress('user@test.com');
        $member->setFirstName($faker->firstName());
        $member->setLastName($faker->lastName());
        $member->setbirthDate($faker->date($format ='Y-m-d', $max = '2004-31-12'));
        $member->setPostalAddress($faker->address());

        $password = $this->encoder->encodePassword($member, 'password');
        $member->setPassword($password);
    }

    $manager->persist($member);
    $manager->flush();
}
}

birthDate 类型是 'date' 并且在你的 setter 中你给出一个字符串 您必须将 setter 更改为此 -> setBirthDate(\DateTimeInterface $birthDate){}