文件包

安装: 54

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

v1.0 2016-03-02 10:36 UTC

This package is not auto-updated.

Last update: 2024-09-24 17:30:39 UTC


README

要求

  • php 7
  • symfony 3

安装

  1. composer.json 中指定包和仓库链接
{
    // ...
    "require": {
        // ...
        "SmartInformationSystems/file-bundle": "dev-master"
    },
    "repositories": [
        {
            "type" : "vcs",
            "url" : "https://github.com/SmartInformationSystems/FileBundle.git"
        }
    ]
}
  1. app/AppKernel.php 中包含此包
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new SmartInformationSystems\FileBundle\SmartInformationSystemsFileBundle(),
            // ...
        );
    }
}
  1. app/config/config.yml 中指定配置
smart_information_systems_file:
    storage:
        type: filesystem
        params:
            path: '%kernel.root_dir%/../web/storage'
            url: 'https:///storage'

使用

将图片添加到实体

  1. 在 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;
}
  1. 将更改应用到数据库

php binv/console doctrine:schema:update

将图片集合添加到实体

  1. 添加新的实体
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;
}
  1. 将更改应用到数据库

php binv/console doctrine:schema:update

在 Sonata Admin 中输出图片

元素列表

  1. 在 Admin 类中添加字段
class BrandAdmin extends AbstractAdmin
{
    $listMapper->add('logo', 'string', [
        'template' => 'SmartInformationSystemsFileBundle:sonata-admin:list_image.html.twig',
    ]);
}

编辑表单

  1. app/config/config.yml 中添加模板
twig:
    form_themes:
        - 'SmartInformationSystemsFileBundle:Form:fields.html.twig'
  1. 在 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,
        ]);
    }
}