Lemonngirl
Lemonngirl

Reputation: 37

Symfony2, Doctrine - flush() returns format() error

I have a problem with saving data in database. Doctrine command - flush() returns

Error: Call to a member function format() on a non-object in /Users/magdalena/Sites/webping/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php line 53

every time when I try to save it. I checked date format many times. These are date which I would like to save:

$currentDate = new \DateTime('now');
which generate: 
["date"]=>   object(DateTime)#712 (3) {
    ["date"]=>
    string(26) "2015-05-25 11:57:19.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(13) "Europe/Warsaw"   }

What is wrong with it?

This is the function which saving data:

$this->setUserId($data['userId']);
            $this->setPackageName($data['packageName']);
            $this->setPackageId($data['packageId']);
            $this->setUnitPrice($data['unitPrice']);
            $this->setService($data['service']);
            $this->setSms($data['sms']);
            $this->setUpgrade(0);
            $this->setDate($currentDate);
            $this->setEndDate($data['endDate']);
            $this->setPaymentType($data['paymentType']);

            $this->setCompanyName($user['companyName']);
            $this->setCity($user['city']);
            $this->setAddress($user['address']);
            $this->setPostalCode($user['postalCode']);
            $this->setNip($user['nip']);
            $this->setStatus(0);

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

and data:

$data:
array(10) {
  ["userId"]=>
  int(287)
  ["packageName"]=>
  string(8) "standard"
  ["packageId"]=>
  int(7)
  ["sms"]=>
  int(100)
  ["service"]=>
  int(10)
  ["endDate"]=>
  object(DateTime)#742 (3) {
    ["date"]=>
    string(26) "2015-06-27 12:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(13) "Europe/Warsaw"
  }
  ["unitPrice"]=>
  string(5) "59.99"
  ["date"]=>
  object(DateTime)#715 (3) {
    ["date"]=>
    string(26) "2015-05-25 12:15:09.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(13) "Europe/Warsaw"
  }
  ["upgrade"]=>
  NULL
  ["paymentType"]=>
  string(7) "przelew"
}



 $user:
    array(6) {
      ["companyName"]=>
      string(8) "vdbfgbfg"
      ["address"]=>
      string(5) "fgbfg"
      ["city"]=>
      string(5) "bfgbf"
      ["postalCode"]=>
      string(6) "23-098"
      ["nip"]=>
      string(10) "1234563218"
      ["payment"]=>
      string(7) "przelew"
    }

EDIT

I've noticed that this error occurred every time when I flash() data in new code. Even when I don't change any date. Previous code works without errors. This may be related to cache?

Upvotes: 1

Views: 867

Answers (2)

Lemonngirl
Lemonngirl

Reputation: 37

It started to work when I call $em->clear() before set new data and flush(). Is it a good way?

Upvotes: 0

Nawfal Serrar
Nawfal Serrar

Reputation: 2263

CurrentDate looks ok it is a DateTime object which will not be the problem, on the other hand $this->setEndDate($data['endDate']); this field i am not sure about which type is it but i am guessing this is your problem, otherwise show us your entity for more help. and check your endDate if it is a DateTime object or just string.

Upvotes: 1

Related Questions