ozznest / uploadable
用于轻松集成的包,将上传属性上传到实体
v1.1.2
2016-09-09 23:30 UTC
Requires
- php: >=5.4
- doctrine/doctrine-bundle: ~1.3
- symfony/framework-bundle: ~2.3|~3.0
This package is auto-updated.
Last update: 2024-09-08 21:22:52 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); } } }