Doctrine 不是冲洗实体并且返回错误的值,发生了什么?

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

我将这段代码作为 Restful API 方法的一部分:

// $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

这是每个 dump() 语句的输出:

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: "ertvn52@pdi-inc.com.train"
  #first: "Joan"
  #last: "Brooks"
  #title: null
  #bio: null
  #phone: null
  #email: "jbrooks@pdi-inc.com.train"
  #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: "ertvn52@pdi-inc.com.train"
  #first: "Joan"
  #last: "Brooks"
  #title: null
  #bio: null
  #phone: null
  #email: "jbrooks@pdi-inc.com.train"
  #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
}

如果我在 DB 运行 这个查询 SELECT repTokenId, updatedAt FROM reps WHERE rep_id='005800000039cQbAAI' 我得到这个结果:

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

为什么实体返回了错误的值?为什么正确的 $xsession 值没有更新?为什么 $entRep->getRepTokenId() 会给我一个 unknow 值,而这个值在会话变量或任何其他地方都不存在?这里发生了什么?

这很奇怪。开始时,来自数据库的 repTokenId 为空(在行 114 中转储)。在持久化您的实体(转储 125 行)和数据库(您的 SELECT sql)之后,其值不同于 null。结论:Doctrine 刷​​新实体,因为如果不是的话,你将在 db.null 中拥有

你在 db 中的新值是你的令牌,但它以某种方式被截断了。所以我会检查两件事:

  1. 数据库中 repTokenId 字段的长度。这么长的值够长吗?
  2. 实体定义中 repTokenId 字段的长度