ozznest/uploadable

Bundle for easy integrations upload properties to entity

Installs: 3 584

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 3

Type:symfony-bundle

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

This package is auto-updated.

Last update: 2024-12-08 21:53:19 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);
        }
    }
}