youshido/uploadable

用于将上传属性轻松集成到实体中的包

安装次数: 3,051

依赖者: 2

建议者: 0

安全性: 0

星标: 1

关注者: 4

分支: 3

类型:symfony-bundle

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

This package is not auto-updated.

Last update: 2024-09-14 17:37:32 UTC


README

此包为Symfony2/3 Doctrine实体提供了可上传的行为

通过Composer安装

composer require youshido/uploadable

在AppKernel.php中启用

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\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);
        }
    }
}