Symfony2 File Upload with Related Entities

Here is how I do it, thank me later.

What it does? Upload an image file and an audio file and add an encode job.
The purpose is to join an image and a mp3 into a mkv file x264 encoded with ffmpeg.
But this here is only to upload the files and add the related entities. Yes I am very proud of myself for having accomplished this even if it was extremely more complicated than just doing it in PHP.

Entities:

Encodejob

id;
}

/**
* Set video
*
* @param string $video
* @return Encodejob
*/
public function setVideo($video)
{
$this->video = $video;

return $this;
}

/**
* Get video
*
* @return string
*/
public function getVideo()
{
return $this->video;
}

/**
* Set finished
*
* @param boolean $finished
* @return Encodejob
*/
public function setFinished($finished)
{
$this->finished = $finished;

return $this;
}

/**
* Get finished
*
* @return boolean
*/
public function getFinished()
{
return $this->finished;
}

/**
* Set image
*
* @param \Dalu\MediaBundle\Entity\Image $image
* @return Encodejob
*/
public function setImage(\Dalu\MediaBundle\Entity\Image $image = null)
{
$this->image = $image;

return $this;
}

/**
* Get image
*
* @return \Dalu\MediaBundle\Entity\Image
*/
public function getImage()
{
return $this->image;
}

/**
* Set audio
*
* @param \Dalu\MediaBundle\Entity\Audio $audio
* @return Encodejob
*/
public function setAudio(\Dalu\MediaBundle\Entity\Audio $audio = null)
{
$this->audio = $audio;

return $this;
}

/**
* Get audio
*
* @return \Dalu\MediaBundle\Entity\Audio
*/
public function getAudio()
{
return $this->audio;
}

public function setUploadedAudio(UploadedFile $uploaded_audio = null)
{
$this->uploaded_audio = $uploaded_audio;
return $this;
}

public function getUploadedAudio()
{
return $this->uploaded_audio;
}

public function setUploadedImage(UploadedFile $uploaded_image = null)
{
$this->uploaded_image = $uploaded_image;
return $this;
}

public function getUploadedImage()
{
return $this->uploaded_image;
}
}

Image

path ? null : $this->getUploadRootDir() . '/' . $this->path;
}

public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path;
}

protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}

protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'media/image';
}

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set filename
*
* @param string $filename
* @return Image
*/
public function setFilename($filename)
{
$this->filename = $filename;

return $this;
}

/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}

/**
* Set path
*
* @param string $path
* @return Image
*/
public function setPath($path)
{
$this->path = $path;

return $this;
}

/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
}

Audio

path ? null : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
}

public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir() . '/' . $this->path;
}

protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}

protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'media/audio';
}

/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}

/**
* Set filename
*
* @param string $filename
* @return Image
*/
public function setFilename($filename)
{
$this->filename = $filename;

return $this;
}

/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}

/**
* Set path
*
* @param string $path
* @return Image
*/
public function setPath($path)
{
$this->path = $path;

return $this;
}

/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
}

Form

add('video','text',array(
'attr' => array(
'class'=> 'input-xxlarge'
)))
->add('uploaded_image','file',array(
'attr' => array(
'class'=> 'input-xxlarge'
)))
->add('uploaded_audio','file',array(
'attr' => array(
'class'=> 'input-xxlarge'
)))
;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Dalu\MediaBundle\Entity\Encodejob'
));
}

public function getName()
{
return 'dalu_mediabundle_encodejobtype';
}
}

Controller

getDoctrine()->getRepository('DaluMediaBundle:Encodejob')->findAll();
return array(
'jobs' => $jobs
);
}

/**
* @Route("/new", name="media_encode_new")
* @Template()
*/
public function newAction()
{
$entity = new Encodejob();
$form = $this->createForm(new EncodejobType(), $entity);

return array(
'entity' => $entity,
'form' => $form->createView()
);
}

/**
* @Route("/create", name="media_encode_create")
* @Method("POST")
* @Template()
*/
public function newpostAction(Request $request)
{
$entity = new Encodejob();
$form = $this->createForm(new EncodejobType(), $entity);

$form->bind($request);

if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$image = new Image();
$audio = new Audio();

$uploaded_image = $form->get('uploaded_image')->getData();
$uploaded_audio = $form->get('uploaded_audio')->getData();

$image->setFilename($uploaded_image->getClientOriginalName()); //TODO: Bild umbenennen
$audio->setFilename($uploaded_audio->getClientOriginalName()); //TODO: Audio umbenennen

$date = new \DateTime();
$image->setPath($date->format('Y/m/d'));
$audio->setPath($date->format('Y/m/d'));

$uploaded_image->move($image->getAbsolutePath());
$uploaded_audio->move($audio->getAbsolutePath());

$entity->setImage($image);
$entity->setAudio($audio);

$em->persist($image);
$em->persist($audio);

$em->persist($entity);
$em->flush();

return $this->redirect($this->generateUrl('media_encode_index'));
}

return array(
'entity' => $entity,
'form' => $form
);
}

}

new.html.twig

{% extends 'DaluThemeBundle::bootstrap.html.twig' %}
{% block body %}

{{ form_widget(form) }}

{% endblock %}

Beteilige dich an der Unterhaltung

1 Kommentar

Schreib einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.