youshido/uploadable

Bundle for easy integrations upload properties to entity

Installs: 3 051

Dependents: 2

Suggesters: 0

Security: 0

Stars: 1

Watchers: 4

Forks: 3

Type:symfony-bundle

v1.1.2 2016-09-09 23:30 UTC

This package is not auto-updated.

Last update: 2024-10-26 18:12:03 UTC


README

Bundle brings uploadable behavor for Symfony2/3 Doctrine Entities

Install via Composer:

composer require youshido/uploadable

Enable in your AppKernel.php:

new Youshido\UploadableBundle\UploadableBundle(),

Add annotation to your entity:

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;

Use in controller:

$post = new Post();

$form = $this->createFormBuilder($post, ['action' => $this->generateUrl('example1')])
    ->add('path', 'Youshido\UploadableBundle\Type\UploadableFileType', ['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);
        }
    }
}