i wonder how can create multiple file uploading without using collection field type. thing is: have class , class need link images. don't need descriptions , other kind of information in these images, create additional field in main entity. add
->add('files', 'file', array( 'label' => 'images', 'required' => false, 'attr' => array( 'accept' => 'image/*', 'multiple' => true )
in it's form class , this:
/** * @orm\prepersist() * @orm\preupdate() */ public function preupload() { if (!empty($this->files)) { if (!empty($this->images)) { $this->insertingkey = $this->images->count(); } foreach ($this->files $file) { $imagename = uniqid('entity_') . '_' . date('y-m-d_h:i') . '.' . $file->guessextension(); if ($this->images === null) { $this->images = new arraycollection(); } $this->images->add($imagename); } } } /** * @orm\postpersist() * @orm\postupdate() */ public function upload() { if (empty($this->files)) { return; } if ($this->insertingkey) { foreach ($this->files $file) { $file->move($this->getuploadrootdir(), $this->images[ $this->insertingkey++ ]); } } else { foreach ($this->files $key => $file) { $file->move($this->getuploadrootdir(), $this->images[ $key ]); } } }
as doctrine events in entity class, , of course make file filed work , array. problem - can't add images second time. instance, when i've upload images , decided upload more - upload physically(i can see them in project directory), nothing's changed on page.
could give me advice guys? or maybe there other methods uploading many files , having 1 form field @ same time? great thanks.
i've solved problem way:
this code entity
/** * @orm\prepersist() * @orm\preupdate() */ public function preupload() { if ($this->files[0] != null || !$this->files) { if (!empty($this->images)) { $this->insertingkey = count($this->images); } foreach ($this->files $file) { $imagename = uniqid('pref_') . '_' . date('y-m-d_h:i') . '.' . $file->guessextension(); if ($this->images === null) { $this->images = array(); } $this->images[] = $imagename; } } } /** * @orm\postpersist() * @orm\postupdate() */ public function upload() { if ($this->files[0] == null || !$this->files) { return; } if ($this->insertingkey) { foreach ($this->files $file) { $file->move($this->getuploadrootdir(), $this->images[ $this->insertingkey++ ]); } } else { foreach ($this->files $key => $file) { $file->move($this->getuploadrootdir(), $this->images[ $key ]); } } } public function getimageswithabsolutepath() { if (!empty($this->images)) { $images = array(); foreach ($this->images $image) { $images[$image] = $this->getuploadrootdir() . '/' . $image; } return $images; } return null; } public function getimageswithrelativepath() { if (!empty($this->images)) { $images = array(); foreach ($this->images $image) { $images[$image] = $this->getuploaddir() . '/' . $image; } return $images; } return null; } public function getuploadrootdir() { return __dir__ . '/../../../../web/' . $this->getuploaddir(); } public function getuploaddir() { return 'images/venue'; } public function removeimage($imagename) { if ($this->images && in_array($imagename, $this->images)) { $key = array_search($imagename, $this->images); unset($this->images[$key]); if (file_exists($this->getuploadrootdir() . '/' . $imagename)) { unlink($this->getuploadrootdir() . '/' . $imagename); } $this->images = array_values($this->images); } }
this piece of code controller:
if ($request->getmethod() === "post") { $form->bind($request); if ($form->isvalid()) { $deleteimages = $request->request->get('delete_thumb', array()); if (!empty($deleteimages)) { foreach ($request->request->get('delete_thumb') $image) { $imagename = substr($image, strrpos($image, '/') + 1); $venue->removeimage($imagename); } } $this->persist($venue, true); if ($request->request->get('submit-and-quit') !== null) { return $this->redirecttoroute('admin_list'); } return array('form' => $form->createview()); } }
Comments
Post a Comment