smart-information-systems / file-bundle
File bundle
Installs: 57
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 2
Open Issues: 0
Type:symfony-bundle
Requires
- imagine/imagine: ~0.6.0
This package is not auto-updated.
Last update: 2025-05-06 20:21:06 UTC
README
Требования:
- php 7
- symfony 3
Установка
- Прописать пакет и ссылку на репозиторий в
composer.json
{ // ... "require": { // ... "SmartInformationSystems/file-bundle": "dev-master" }, "repositories": [ { "type" : "vcs", "url" : "https://github.com/SmartInformationSystems/FileBundle.git" } ] }
- Включить бандл в
app/AppKernel.php
class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new SmartInformationSystems\FileBundle\SmartInformationSystemsFileBundle(), // ... ); } }
- Прописать настройки в
app/config/config.yml
smart_information_systems_file: storage: type: filesystem params: path: '%kernel.root_dir%/../web/storage' url: 'http://localhost/storage'
Использование
Добавление картинки в сущность
- Добавляем поле в Entity
use SmartInformationSystems\FileBundle\Entity\Image; use SmartInformationSystems\FileBundle\Annotations as Files; class Brand { /** * Логотип * * @var Image * * @ORM\OneToOne(targetEntity="SmartInformationSystems\FileBundle\Entity\Image", cascade="all") * @ORM\JoinColumn(name="logo_id", referencedColumnName="id", nullable=true) * * @Files\Image( * width=370, height=210, * previews={ * @Files\Image\Preview(name="admin", width=100, height=100), * } * ) */ private $logo; }
- Применяем изменения в БД
php binv/console doctrine:schema:update
Добавление коллекции картинок в сущность
- Добавляем новую сущность
use SmartInformationSystems\FileBundle\Entity\Image; use SmartInformationSystems\FileBundle\Annotations as Files; class ProductImage { /** * Товар * * @var Product * * @ORM\ManyToOne(targetEntity="Product", inversedBy="images") * @ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=false) */ private $good; /** * Изображение. * * @var Image * * @ORM\OneToOne(targetEntity="SmartInformationSystems\FileBundle\Entity\Image", cascade="all") * @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=false) * * @Files\Image( * width=700, height=700, * previews={ * @Files\Image\Preview(name="admin", width=100, height=100, crop=true), * @Files\Image\Preview(name="small", width=65, height=65, crop=true), * @Files\Image\Preview(name="medium", width=250, height=250, crop=true) * } * ) */ private $image; }
- Применяем изменения в БД
php binv/console doctrine:schema:update
Вывод изображени в Sonata Admin
Список элементов
- Добавляем поле в Admin класс
class BrandAdmin extends AbstractAdmin { $listMapper->add('logo', 'string', [ 'template' => 'SmartInformationSystemsFileBundle:sonata-admin:list_image.html.twig', ]); }
Форма редактирования
- Добавить шаблон в
app/config/config.yml
twig: form_themes: - 'SmartInformationSystemsFileBundle:Form:fields.html.twig'
- Добавить поле в Admin класс
use SmartInformationSystems\FileBundle\Form\Type\FileType; class BrandAdmin extends AbstractAdmin { protected function configureFormFields(FormMapper $formMapper) { $formMapper->add('logo', FileType::class, [ 'entity_class' => Brand::class, 'data_class' => Image::class, 'required' => false, ]); } }