Bundle brings uploadable behavor for Symfony2/3 Doctrine Entities
composer require youshido/uploadable
new Youshido\UploadableBundle\UploadableBundle(),
use Youshido\UploadableBundle\Annotations as Youshido;
class Post
{
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*
* @Youshido\Uploadable(override="true", asserts={@Assert\File(
* maxSize = "1024k",
* mimeTypes = {"image/jpeg"},
* mimeTypesMessage = "Please upload a valid Image"
* )})
*/
private $path;
$post = new Post();
$form = $this->createFormBuilder($post, ['action' => $this->generateUrl('example1')])
->add('path', 'youshido_file', ['entity_class' => 'AppBundle\Entity\Post'])
->add('submit', 'submit')
->getForm();
$form->handleRequest($request);
if($form->isValid()){
$this->getDoctrine()->getManager()->persist($post);
$this->getDoctrine()->getManager()->flush();
}
if($request->getMethod() == 'POST'){
if($file = $request->files->get('path')){
if($post = $this->getDoctrine()->getRepository('AppBundle:Post')->find($id)){
$this->get('youshido.uploadable.enity_manager')
->saveFile($post, 'path', $file, true);
}
}
}