Π Π°Π±ΠΎΡΠ° Ρ ΡΠ°ΠΉΠ»Π°ΠΌΠΈ
Π’ΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΡ:
Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°
- ΠΡΠΎΠΏΠΈΡΠ°ΡΡ ΠΏΠ°ΠΊΠ΅Ρ ΠΈ ΡΡΡΠ»ΠΊΡ Π½Π° ΡΠ΅ΠΏΠΎΠ·ΠΈΡΠΎΡΠΈΠΉ Π²
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
, (*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;
}
- ΠΡΠΈΠΌΠ΅Π½ΡΠ΅ΠΌ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ Π² ΠΠ
php binv/console doctrine:schema:update
, (*2)
ΠΡΠ²ΠΎΠ΄ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈ Π² 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,
]);
}
}