Witam,
Mam taką oto akcję update do bazy danych:
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('(...)Bundle:Products')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Products entity.');
}
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$file = $editForm->get('photo')->getData();
$name = $file->getClientOriginalName();
$file->move('../web/bundles/static/img/', $name);
$product = new Products();
$product->setName( $editForm->get('name'));
$product->setPhoto($name);
$product->setEAN( $editForm->get('eAN'));
$product->setFeatures( $editForm['features']->getData());
$em->persist($product);
$em->flush();
return $this->redirect($this->generateUrl('admin_produkty_edit', array('id' => $id))); }
return $this->render('(..)Bundle:Products:edit.html.twig', array( 'entity' => $entity,
'edit_form' => $editForm->createView(),
));
}
Problem polega na tym jak wykonać update tej funkcji:
$product->setFeatures();
?
formularz wygląda tak:
private function createEditForm($entity)
{
$form = $this->createFormBuilder($entity)
->add('name', 'text', array('label' => 'Nazwa')) ->add('photo', 'file', array('label' => 'Zdjęcie', 'data' => '')) ->add('eAN', 'text', array('label' => 'Kod EAN')) ->add('features' , 'entity', array( 'class' => '(...)Bundle:Features',
'property' => 'name',
'multiple' => true,
"expanded" => true,
'label' => 'Wybierz cechy: ',
))
->add('submit', 'submit', array('label' => 'Zapisz zmiany')) ->getForm();
return $form;
}
a akcja dodawania do bazy tak:
public function createAction(Request $request)
{
$form = $this->createCreateForm();
$form->submit($request);
if ($form->isValid()) {
$data = $form->getData();
$name = $data['photo']->getClientOriginalName(); //nazwa pliku
$data['photo']->move('../web/bundles/static/img/', $name);
$em = $this->getDoctrine()->getManager();
$product = new Products();
$product->setName($data['name']);
$product->setPhoto($name);
$product->setEAN($data['eAN']);
$product->setFeatures($data['features']);
$em->persist($product);
$em->flush();
return $this->redirect($this->generateUrl('produkty'));
}
return $this->render('(...)Bundle:Products:new.html.twig', array( 'form' => $form->createView(),
));
}
w przypadku dodawania do bazy wszystko działa wystarczy że wrzuciłem tablicę do funckji setFeatures() natomiast jak wykonuję update to ta funkcja nie działa. Jak wykonać update do bazy która ma relację m:n i jednocześnie wykonać upload pliku?
pozdrawiam
Ten post edytował mattix19 19.09.2013, 21:21:58