2017-08-08 143 views
0

這是第一次,我需要問一個問題 - 我通常會找到答案..爲什麼JMS更改我的Doctrine Entity布爾值?

我能夠教條實體轉換,並從JSON與JMS串行。我唯一的問題是,當我從JSON反序列化到實體時,JSON中的任何假布爾值:"boolean_value":false將在Doctrine實體中設置爲true

我已經縮小到JMS串行器。數據在此代碼中更改。

public function toEntity($entity_name, $input, $inputFormat = 'json') { 
    // $input is a json string where "boolean_value":false 
    $serializer = SerializerBuilder::create()->build(); 
    $entity = $serializer->deserialize($json, $entity_name, $inputFormat); 
    // the output entity's $boolean_value is now true 
    // $entity->getBooleanValue() === true 
    return $entity; 
} 

讓我知道你是否需要別的東西。

回答

0

事實證明,json_decode不會將字符串值'true'或'false'轉換爲truefalse,因此代碼檢查字符串值是否爲true | false。

PHP: Booleans

我更新了我的toEntity方法來解決這個問題。

public function toEntity($entity_name, array $input, $inputFormat = 'json') { 
    foreach ($input as $k => $v) { 
     if ($v == 'true' || $v == 'false') { 
      $input[$k] = filter_var($v, FILTER_VALIDATE_BOOLEAN); 
     } 
    } 
    $input = json_encode($input); 
    $serializer = SerializerBuilder::create()->build(); 
    $entity = $serializer->deserialize($input, $entity_name, $inputFormat); 
    return $entity; 
}