0

While trying to insert a collection of entities which have a file field, i couldn't figure out if there's is a better way to create the UploadedFile Object being cast by the Document::document annotation. Here's my code, any help on improving it is very appreciated :)

public function createAction(Request $request) {
    $em = $this->getDoctrine()->getManager();

    $user = $this->get('security.context')->getToken()->getUser();
    $entity = new Paper();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {

        $entity->setAuthor($user);
        $em->persist($entity);

        // chotest foreach in the universe
        if (isset($_FILES) && array_key_exists('arkad1a_cfpbundle_paper', $_FILES)) {
            foreach ($_FILES['arkad1a_cfpbundle_paper']['name']['documents'] as $k => $v) {
                $document = new UploadedFile(
                    $_FILES['arkad1a_cfpbundle_paper']['tmp_name']['documents'][$k]['document'],
                    $_FILES['arkad1a_cfpbundle_paper']['name']['documents'][$k]['document'],
                    $_FILES['arkad1a_cfpbundle_paper']['type']['documents'][$k]['document'],
                    $_FILES['arkad1a_cfpbundle_paper']['size']['documents'][$k]['document'],
                    $_FILES['arkad1a_cfpbundle_paper']['error']['documents'][$k]['document'],
                    false
                );

                $Document = new \Arkad1a\CFPBundle\Entity\Document();
                $Document->setAuthor($user)
                        ->setDocument($document)
                        ->setPaper($entity)
                        ->upload();
                $em->persist($Document);
            }
        }

        $em->flush();

        return $this->redirect($this->generateUrl('paper_show', array('id' => $entity->getId())));
    } else {
        die('invalid');
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );
}
nitzer
  • 11
  • 1
  • Go and check my [Symfony2 file upload step-by-step](http://stackoverflow.com/questions/17951294/symfony2-file-upload-step-by-step/17955934#17955934). It should give you some inspiration :) – ferdynator Apr 27 '14 at 19:36
  • Thanks, I created the file upload with that guide, and also followed the collection step by step guide, but the point is merging these two. the uploads tells you to upload while inserting, but not how to iterate through them – nitzer Apr 27 '14 at 19:57
  • Why do you need to iterate through them? The *Symfony2 Form Component* should assign each uploaded file to a separate entity itself. All you need is a `OneToMany` relation and the correct *form type*. – ferdynator Apr 27 '14 at 20:02
  • i think that found the problem, haven't fixed it yet, but apparently i omitted the set method for the collection in the controller. – nitzer Apr 28 '14 at 04:51
  • 1
    also http://stackoverflow.com/questions/17951294/symfony2-file-upload-step-by-step/17955934#17955934 really helped! thanks again – nitzer Apr 28 '14 at 04:58

0 Answers0