Gilberto Albino
Gilberto Albino

Reputation: 2745

Doctrine not flushing null persisted entity object

I am trying to flush a persited entity object, but I am getting this error message:

Fatal error: Call to a member function format() on a non-object in C:\xampp\htdocs\project\vendor\doctrine\dbal\lib\Doctrine\DBAL\Types\DateType.php on line 44

Actually this is the action method:

public function processRegisterFormAction()
{

    $data = filter_var_array($_POST['form'], FILTER_SANITIZE_STRING);
    extract($data);

    $customer  = new Customer();

    $dob = explode('/', $date_of_birth);
    $date_of_birth = $dob[2] . '-' . $dob[1] . '-' . $dob[0];

    $datetime = date('Y-m-d H:i:s');

    $customer->setEmail($email);
    $customer->setPassword($password);
    $customer->setName($name);
    $customer->setGender($gender);
    $customer->setDateOfBirth($date_of_birth);
    $customer->setZipcode($zipcode);
    $customer->setState($state);
    $customer->setCity($city);
    $customer->setDistrict($district);
    $customer->setAddress($address);
    $customer->setStreetNumber($street_number);
    $customer->setCompanyName($company_name);
    $customer->setCreated( $datetime);
    $customer->setLastModified($datetime);

    $em = $this->getDoctrine()->getManager();
    $em->persist($customer);
    $em->flush();

    return new Response('Created Customer ' . $customer->getId() );

}

In my Entity I have declared $dateOfBirth, $created, $lastModified as "String" because I thought it was something related to datetime, but, nope!

And I dumped $em->persist($customer) it return NULL

die(var_dump($em->persist($customer)));

Thanks in Advance!

Upvotes: 0

Views: 775

Answers (1)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

The problem is not related to Doctrine. The answer is in the error message you got.

You're calling format() on a variable that isn't a DateTime object. Make sure your variable is an instance of DateTime before calling format() helper.

Upvotes: 1

Related Questions