假设我有一个名为User的实体:
class User implements UserInterface
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $username;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Address", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $address;
地址字段是与地址实体的OneToOne关系:
class Address
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $street;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
我有一个控制器来更新用户及其地址:
...
public function putUserSelf(Request $request)
{
$em = $this->getDoctrine()->getManager();
$user = $this->getUser();
$encoders = array(new JsonEncoder());
$normalizers = array(new ObjectNormalizer(null, null, null, new ReflectionExtractor()));
$serializer = new Serializer($normalizers, $encoders);
$user = $serializer->deserialize($request->getContent(), User::class, "json", ['deep_object_to_populate' => $user]);
$em->persist($user);
$em->flush();
理论上,我现在应该可以像这样传递一个json:
{
"username": "foo",
"address": {"name": "bar"}
}
更新我的实体。但是问题是,我得到一个SQL错误:
An exception occurred while executing 'INSERT INTO address (street, name) VALUES (?, ?)' with params [null, "bar"]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'street' cannot be null
似乎实体合并无效。
答案 0 :(得分:1)
根据docs
将
AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE
选项设置为 true 时,将从规范化数据中更新根OBJECT_TO_POPULATE
的现有子级,而不是通过非规范化器重新创建它们。 [...] (强调我的)
因此您必须设置一个 extra 选项,因此您的行应为:
$user = $serializer->deserialize($request->getContent(), User::class, "json", [
'object_to_populate' => $user, // this still needs to be set, without the "deep_"
'deep_object_to_populate' => true,
]);
特定组件的源代码中还有一些其他注释:
/** * Flag to tell the denormalizer to also populate existing objects on * attributes of the main object. * * Setting this to true is only useful if you also specify the root object * in OBJECT_TO_POPULATE. */ public const DEEP_OBJECT_TO_POPULATE = 'deep_object_to_populate';