ReynierPM
ReynierPM

Reputation: 18660

Doctrine is not flushing entity and is returning the wrong value, what's happen?

I have this piece of code as part of a Restful API method:

// $soqlObj1['records'][0]['Id'] == "005800000039cQbAAI"

$entRep = $em->getRepository('PDOneBundle:Representative')->find($soqlObj1['records'][0]['Id']);

dump($entRep); // line 114

if ($entRep->getRepTokenId() === null) {
    $xsession = bin2hex(openssl_random_pseudo_bytes(100));
    $entRep->setRepTokenId($xsession);
    $em->flush();

    dump($xsession); // line 121
    dump($entRep->getRepTokenId()); // line 122
}

dump($entRep); // line 125

This are the outputs from each dump() statement:

in SessionRestController.php line 114:

Representative {#2369 ▼
  #rep_id: "005800000039cQbAAI"
  #display_name: "Joan Brooks"
  #avatar_url: "https://pdone.s3.amazonaws.com/avatar/default_avatar.png"
  #rep_type: "VEEVA"
  #username: "[email protected]"
  #first: "Joan"
  #last: "Brooks"
  #title: null
  #bio: null
  #phone: null
  #email: "[email protected]"
  #inactive: false
  #lastLoginAt: DateTime {#2365 ▶}
  #territory: Territory {#2422 ▶}
  #repTokenId: null
  #createdAt: DateTime {#2366 ▶}
  #updatedAt: DateTime {#2367 ▼
    +"date": "2015-08-21 15:03:07.000000"
    +"timezone_type": 3
    +"timezone": "America/New_York"
  }
  -file: null
}

in SessionRestController.php line 121: 
"79e5ead64a2e1d2ba2ecac1b7dbbabb5fd6ec2b2659fb6bc2ae98d613bfc9aae623fd81dfe2c15ef72f1565ed2f41619baf574387a16a6b1138b2b730c75b21a7081587e9b0494c30b557cdc562a517013d6b78d82bbe4af3e71faaa7257e1caf0aa2342"

in SessionRestController.php line 122: 
"79e5ead64a2e1d2ba2ecac1b7dbbabb5fd6ec2b2659fb6bc2ae98d613bfc9aae623fd81dfe2c15ef72f1565ed2f41619baf574387a16a6b1138b2b730c75b21a7081587e9b0494c30b557cdc562a517013d6b78d82bbe4af3e71faaa7257e1caf0aa2342"

in SessionRestController.php line 125: ▶
Representative {#2369 ▼
  #rep_id: "005800000039cQbAAI"
  #display_name: "Joan Brooks"
  #avatar_url: "https://pdone.s3.amazonaws.com/avatar/default_avatar.png"
  #rep_type: "VEEVA"
  #username: "[email protected]"
  #first: "Joan"
  #last: "Brooks"
  #title: null
  #bio: null
  #phone: null
  #email: "[email protected]"
  #inactive: false
  #lastLoginAt: DateTime {#2365 ▶}
  #territory: Territory {#2422 ▶}
  #repTokenId: "79e5ead64a2e1d2ba2ecac1b7dbbabb5fd6ec2b2659fb6bc2ae98d613bfc9aae623fd81dfe2c15ef72f1565ed2f41619baf574387a16a6b1138b2b730c75b21a7081587e9b0494c30b557cdc562a517013d6b78d82bbe4af3e71faaa7257e1caf0aa2342"
  #createdAt: DateTime {#2366 ▶}
  #updatedAt: DateTime {#2393 ▼
    +"date": "2015-08-21 15:11:18.000000"
    +"timezone_type": 3
    +"timezone": "America/New_York"
  }
  -file: null
}

If I run this query SELECT repTokenId, updatedAt FROM reps WHERE rep_id='005800000039cQbAAI' at DB I got this result:

+--------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| repTokenId                                                                                                                                             | updatedAt           |
+--------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+
| 79e5ead64a2e1d2ba2ecac1b7dbbabb5fd6ec2b2659fb6bc2ae98d613bfc9aae623fd81dfe2c15ef72f1565ed2f41619baf574387a16a6b1138b2b730c75b21a7081587e9b0494c30b557c | 2015-08-21 15:11:18 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+

Why the entity is returning the wrong values? Why the right $xsession value isn't updated? Why $entRep->getRepTokenId() is giving me a unknow value that doesn't exists in session vars or any other place? What is happening here?

Upvotes: 0

Views: 1083

Answers (1)

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10890

It's odd. At beginning your repTokenId coming from db is null (dump in line 114). After persist your entity (dump in line 125) and db (your SELECT sql) has value different than null. Conclusion: Doctrine is flushing entity, becase if it didn't you would have null in db.

Your new value in db is your token but it got truncated somehow. So I would check two things:

  1. Lenght of repTokenId field in db. Is it long enough to have such long value?
  2. Length of repTokenId field in entity definition

Upvotes: 1

Related Questions