PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

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

 March 05, 2022     doctrine-orm, faker, php, symfony     No comments   

Issue

I created fixtures with fakerphp to generate fake birth date but can't load fixtures files and getting this error message : Could not convert PHP value '1970-01-01' to type date. Expected one of the following types: null, DateTime. Do you have an idea what I should modify please ?

Here is part of my php entity file code :


/**
 * @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;
}

And my fixture :

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();
}
}

Solution

birthDate type is 'date' and in your setter you give a String you have to change your setter to this -> setBirthDate(\DateTimeInterface $birthDate){}



Answered By - Ouss Ma L'aire Bien
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing