symfony2上的数组集合持久性
我在表单提交时使用一个事件侦听器,我需要捕获一个xml文件,打开它并提取其内容,将其放在一个实体上,然后将其添加到来自其他实体的集合中 现在,这是一个工程:symfony2上的数组集合持久性,symfony,doctrine-orm,Symfony,Doctrine Orm,我在表单提交时使用一个事件侦听器,我需要捕获一个xml文件,打开它并提取其内容,将其放在一个实体上,然后将其添加到来自其他实体的集合中 现在,这是一个工程: $builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){ $entity = $event->getData(); if($entity){ $parent = $event
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){
$entity = $event->getData();
if($entity){
$parent = $event->getForm()->getParent()->getData();
$gpx = $entity['gpx'];
if($gpx){
$xmlGpx = simplexml_load_file($gpx);
foreach ($xmlGpx->wpt as $pt) {
$point = new MonitoringPoint();
$point->setPoint(new \CrEOF\Spatial\PHP\Types\Geometry\Point((string) $pt['lat'], (string) $pt['lon']));
$point->setAltitude((float) $pt->ele);
$point->setDate(($pt->time->count() ? new \DateTime((string)$pt->time) : null ));
$point->setAccuracy((float) $pt->hdop);
$parent->addMonitoringPoint($point);
}
$fileName = $gpx->getClientOriginalName();
$directory = __DIR__.'/../../../../web/uploads/';
$date = new \DateTime();
$newFileName = md5($gpx->getClientOriginalName().$date->getTimestamp());
$gpx->move($directory, $fileName);
$fs = new Filesystem();
$fs->rename($directory.$fileName, $directory.$newFileName.'.gpx');
$parent->setGpx($newFileName.'.gpx');
}
}
});
$parent
是监控
的一个实例,如果打开$parent
,我将看到变量的集合监控点
上添加了$point
变量,gpx也是
然后我去看看实体在被持久化之前,在newAction中
$entity = new Monitoring($params);
$form = $this->createForm(new MonitoringType(), $entity, array(
'action' => $this->generateUrl('my_route'),
'method' => 'POST',
));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
dump($entity);die;
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
}
收藏是空的!但是gpx属性包含正确的值。
集合是否重置?我必须在会话中将点传递到数组中,但仍然认为这不是最好的选择,但效果不错
$array = [];
foreach ($xmlGpx->wpt as $pt) {
$point = new MonitoringPoint();
$point->setPoint(new \CrEOF\Spatial\PHP\Types\Geometry\Point((string) $pt['lat'], (string) $pt['lon']));
$point->setAltitude((float) $pt->ele);
$point->setDate(($pt->time->count() ? new \DateTime((string)$pt->time) : null ));
$point->setAccuracy((float) $pt->hdop);
$point->setMonitoring($parent);
array_push($array, $point);
}
$session = new Session();
$session->getFlashBag()->add('array', $array);
在新行动中:
$em = $this->getDoctrine()->getManager();
$session = new Session();
$array = $session->getFlashBag()->get('array');
foreach($array[0] as $point) {
$point->setMonitoring($entity);
$entity->addMonitoringPoint($point);
}
$em->persist($entity);
$em->flush();
不知道为什么数组在控制器上被重置,因为我在提交过程中设置了实体中的点请务必阅读并理解-我很确定这是您的问题。