it-blaster / attach-file-bundle
Symfony2 扩展包。具有将文件附加到表单的能力。
Requires
- propel/propel1: ~1.6
- sonata-project/admin-bundle: >=2.3
- sonata-project/propel-admin-bundle: dev-master
- symfony/symfony: >=2.2
This package is not auto-updated.
Last update: 2024-09-14 17:31:53 UTC
README
辅助扩展包,用于在网站上处理文件。可以附加多个文件到一个实体。支持多语言版本。
安装
在 composer.json
中添加 ItBlasterAttachFileBundle
{ "require": { "it-blaster/attach-file-bundle": "dev-master" }, }
现在运行 composer,使用以下命令下载扩展包:
$ php composer.phar update it-blaster/attach-file-bundle
Composer 会将扩展包安装到项目文件夹的 vendor/it-blaster/attach-file-bundle
目录。
接下来,在 AppKernel.php
中连接扩展包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new ItBlaster\AttachFileBundle\ItBlasterAttachFileBundle(), ); }
在 app/config/config.yml
中指定行为类 it_blaster_file 的路径,并连接模板视图 attach_file
propel: ... behaviors: ... it_blaster_file: ItBlaster\AttachFileBundle\Behavior\AttachFileBehavior twig: form: resources: - 'ItBlasterAttachFileBundle:Form:attach_file_widget.html.twig' assetic: bundles: - 'ItBlasterAttachFileBundle'
在 app/config/routing.yml
中连接扩展包的路由文件
attach_file:
resource: '@ItBlasterAttachFileBundle/Resources/config/routing.yml'
使用方法
在 schema.yml
文件中连接行为 it_blaster_file
<table name="example"> <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" /> <column name="title" type="varchar" required="true" primaryString="true" /> <column name="image" type="integer" /> <behavior name="it_blaster_file" > <parameter name="file_columns" value="image" /> </behavior> </table>
在 file_columns 参数中指定图像字段的名称。在本例中,该字段是 image。如果需要将多个文件附加到实体,则需要在 file_columns 参数中用逗号分隔字段名称,例如:
<table name="example"> <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" /> <column name="title" type="varchar" required="true" primaryString="true" /> <column name="logo" type="integer" /> <column name="sheet" type="integer" /> <behavior name="it_blaster_file" > <parameter name="file_columns" value="logo, sheet" /> </behavior> </table>
文件字段应具有 integer 类型
接下来,在表单编辑描述中连接文件附加字段
$formMapper ->add('image_file', 'attach_file', array( 'label' => 'Изображение', 'required' => false, 'constraints' => [ new Image([ 'mimeTypes' => [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png' ] ]) ] ));
请注意,字段名称不是 image
,而是 image_file
。这个 image_file
字段以及相应的 get 和 set 方法是由行为 AttachFileBehavior 创建的,它们仅用于表单编辑。
使用多语言
如果您使用基于 propel 行为 i18n
的网站多语言版本,并且需要将文件附加到每个翻译中,则需要在主表(document)中指定参数 i18n
,其中包含文件字段的名称,并在相应的翻译表(document_i18n)中指定参数 file_columns
,其中包含相同的文件字段值。示例:
<table name="document" description="Документ"> <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" /> <column name="file_title" type="varchar" required="true" primaryString="true" /> <column name="active" type="boolean" defaultValue="true" /> <column name="download" type="integer"/> <behavior name="i18n"> <parameter name="i18n_columns" value="file_title, active, download" /> </behavior> <behavior name="it_blaster_i18n"> <parameter name="primary_string" value="file_title" /> </behavior> <behavior name="it_blaster_file" > <parameter name="i18n" value="download" /> </behavior> </table> <table name="document_i18n"> <behavior name="it_blaster_file" > <parameter name="file_columns" value="download" /> </behavior> </table>
接下来,在管理表单中连接 attach_file 视图
$formMapper ->add('DocumentI18ns', new TranslationCollectionType(), [ 'label' => false, 'required' => false, 'type' => new TranslationType(), 'languages' => $this->getConfigurationPool()->getContainer()->getParameter('locales'), 'options' => [ 'label' => false, 'data_class' => 'Artsofte\MainBundle\Model\DocumentI18n', 'columns' => [ 'active' => [ 'label' => "Опубликовать", 'type' => 'checkbox', ], 'file_title' => [ 'label' => "Имя файла", 'type' => 'text', 'required' => true, ], 'download_file' => array( 'type' => 'attach_file', 'label' => 'Выберите файл', 'maxSize' => '20M', 'options' => [ 'sonata_help' => 'Допустимые типы файлов: pdf, doc, docx, zip, jpg, gif, png', 'constraints' => [ new \Symfony\Component\Validator\Constraints\File([ 'mimeTypes' => [ 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.oasis.opendocument.text', 'application/zip', 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png' ] ]) ] ] ), ] ] ]) ;
致谢
It-Blaster it-blaster@yandex.ru