1

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?

Lemonngirl
  • 37
  • 1
  • 10
  • 1
    How are you trying to save the object? Please add some more code. – Fracsi May 25 '15 at 10:12
  • possible duplicate of [Doctrine 2: Call to a member function format() on a non-object ... in DateTimeType.php](http://stackoverflow.com/questions/3378748/doctrine-2-call-to-a-member-function-format-on-a-non-object-in-datetimety) – Federkun May 25 '15 at 10:14
  • not exactly, because I used new \DateTime("now") to create currentDate variable despite of this part returns error. – Lemonngirl May 25 '15 at 10:19
  • Any mapping information for the entity? Also a bit strange that you are saving `$this`. The code is from the controller? – Fracsi May 25 '15 at 10:20
  • no, this function is in preorder entity. I use it in another place, date is generate in the same way and it works but in this case i have w problem. – Lemonngirl May 25 '15 at 10:22
  • Please give us the full code. Method calls in the order you use them. – Fracsi May 25 '15 at 10:26
  • Avoid placing repository-related functional inside entity! For such data I recommend you use Symfony Forms component. It is much easier and you will have less problems. – Michael Sivolobov May 25 '15 at 10:50
  • @Lemonngirl: are you using date as primary key? – DonCallisto May 25 '15 at 12:50
  • no. The error occurred even if I update data without dateTime variable. Each new flush() returns error. Why the old code works, the new no depsite of they are the same? It could be related to the cache or sth like this? I removed cache and nothing has changed. – Lemonngirl May 26 '15 at 07:32

2 Answers2

1

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.

Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • this is endDate: ["endDate"]=> object(DateTime)#742 (3) { ["date"]=> string(26) "2015-06-27 12:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Warsaw" } – Lemonngirl May 25 '15 at 10:23
0

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

Lemonngirl
  • 37
  • 1
  • 10