"Alex", "id" => 1) array("name-6ren">
gpt4 book ai didi

symfony - 使用 Doctrine2 从数组到对象

转载 作者:行者123 更新时间:2023-12-05 09:23:18 25 4
gpt4 key购买 nike

鉴于我有一个包含标量值(我信任)的数组,我如何将它们转换为学说实体?

例如:

array(
array("name" => "Alex", "id" => 1)
array("name" => "Chris", "id" => 2)
)

到一组用户实体。

最佳答案

我知道的唯一方法是做这样的事情:

// loop over the array
foreach ($users as $user) {
// new entity
$post = new User();

// now loop over the properties of each post array...
foreach ($user as $property => $value) {
$method = sprintf('set%s', $property);
// use the method as a variable variable to set your value
$post->$method($value);
}

// persist the entity
$em->persist($post);
}

关于symfony - 使用 Doctrine2 从数组到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23011588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com