我从API中提取一些数据,并将其设置为如下数组:
array (size=8)
'name' => string 'Application Name'
'description' => string 'Application Description'
'author' => string 'Author name'
'version' => string '1.0.0'
'ip' => string '127.1.1'
'host' => string 'http://www.host.com'
'image' => string 'http://www.hostc.com/image.png'
'route' =>
array
1 =>
array
'name' => string 'route'
'description' => string 'The description of route'
'route' => string 'http://www.myroute.com/route'
2 =>
array
'name' => string 'route'
'description' => string 'The description of route'
'route' => string 'http://www.myroute.com/route'
3 =>
'name' => string 'route'
'description' => string 'The description of route'
'route' => string 'http://www.myroute.com/route'
此数组将我的Application Entity及其OneToMany连接反映到ApplicationRoute实体。
我的问题是这个。有没有一种简单的方法将它转换为可用的对象并坚持使用Doctrine?
修改
我的工作解决方案就是:
我在我的Application实体中创建了一个名为:fromArray
的函数public function fromArray($array)
{
$this->setName($array['name']);
$this->setDescription($array['description']);
$this->setAuthor($array['author']);
$this->setIp($array['ip']);
$this->setHost($array['host']);
$this->setImageUrl($array['image']);
$routeCollection = new ArrayCollection();
foreach ( $array['route'] AS $route) {
$routeObejct = new ApplicationRoutes();
$routeObejct->setName($route['name']);
$routeObejct->setDescription($route['description']);
$routeObejct->setRoute($route['route']);
$routeCollection[] = $routeObejct;
}
$this->setRoute($routeCollection);
return $this;
}
这样做,但我觉得有更好的方法......
任何建议,非常感谢!