<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ashleydawson / doctrine-flysystem-bundle example snippets
// app/AppKernel.php
$bundles = array(
// ...,
);
// Do this after the production bundles are set
\AshleyDawson\DoctrineFlysystemBundle\AshleyDawsonDoctrineFlysystemBundle::registerInto($bundles);
// ...
// app/AppKernel.php
// ...
$bundles = array(
// ...
new Oneup\FlysystemBundle\OneupFlysystemBundle(), // Doctrine Flysystem Bundle depends on this
new AshleyDawson\DoctrineFlysystemBundle\AshleyDawsonDoctrineFlysystemBundle(),
);
// ...
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AshleyDawson\DoctrineFlysystemBundle\ORM\StorableTrait;
/**
* Post
*
* @ORM\Table()
* @ORM\Entity
*/
class Post
{
/**
* Use the storable file trait
*/
use StorableTrait;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Get the Flysystem filesystem mount prefix as
* configured in https://github.com/1up-lab/OneupFlysystemBundle/blob/master/Resources/doc/filesystem_create.md#use-the-mount-manager
*
* <code>
* // A single filesystem...
*
* public function getFilesystemMountPrefix()
* {
* return 'example_filesystem_mount_prefix';
* }
*
* // Or a list of filesystems...
*
* public function getFilesystemMountPrefix()
* {
* return [
* 'example_filesystem_mount_prefix_01',
* 'example_filesystem_mount_prefix_02',
* ];
* }
* </code>
*
* @return string|array
*/
public function getFilesystemMountPrefix()
{
return 'my_filesystem_mount_name'; // This is the mount prefix configured in app/config/config.yml
}
}
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Class PostType
*
* @package Acme\DemoBundle\Form
*/
class PostType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('uploaded_file', 'file', [
'
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AshleyDawson\DoctrineFlysystemBundle\ORM\StorableTrait;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Post
*
* @ORM\Table()
* @ORM\Entity
*/
class Post
{
/**
* Use the storable file trait
*/
use StorableTrait;
// ...
/**
* Set my file
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
* @return $this
*/
public function setMyFile(UploadedFile $file = null)
{
$this->setUploadedFile($file);
return $this;
}
}
// ...
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', 'text')
->add('my_file', 'file', [
'
// Replace the file storage path with a random md5 hash directory structure, name and file extension
$this->get('event_dispatcher')->addListener(StorageEvents::PRE_STORE, function (StoreEvent $event) {
// Build a directory structure like "af/9e"
$fileStoragePath = implode('/', str_split(substr(md5(mt_rand()), 0, 4), 2));
$event->setFileStoragePath(sprintf('/%s/%s.%s', $fileStoragePath, md5(mt_rand()), $event->getUploadedFile()->getClientOriginalExtension()));
});