2014-04-11 81 views
0

鑑於我有一個包含標量值(我相信)的數組,我將如何將它們轉換爲學說實體?水合物從數組到對象與Doctrine2

例如:

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

到用戶實體的陣列。

回答

1

我所知道的唯一的辦法是做這樣的事情:

// 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', ucwords($property)) 
     // use the method as a variable variable to set your value 
     $user->$method($value); 
    } 

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