dev-master
9999999-devFile handling by doctrine listener
MIT
The Requires
- php >=5.5.9
- symfony/symfony >=2.6
1.0.0.x-dev
1.0.0.9999999-devFile handling by doctrine listener
MIT
The Requires
- php >=5.5.9
- symfony/symfony >=2.6
Wallogit.com
2017 © Pedro Peláez
File handling by doctrine listener
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*1)
$ composer require jimmytournemaine/file-bundle "master"
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*2)
Then, enable the bundle by adding it to the list of registered bundles
in the app/AppKernel.php file of your project:, (*3)
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new JT\FileBundle\JTFileBundle(),
);
// ...
}
// ...
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class Post
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Avatar", cascade={"persist","remove"})
*/
private $avatar;
}
Be carefull ! Don't forget to use cascade={"persist"} if you want your file to be uploaded.
For same reasons, use cascade={"remove"} to delete the file entity and the file itself when you delete the Post entity., (*4)
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JT\FileBundle\Entity\File;
/**
* @ORM\Entity
*/
class Avatar extends File
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function getTargetDirectory()
{
return 'avatar';
}
}
The getTargetDirectory() method must return the directory where files will be stored. In this example it would be /web/avatar. You should create the directory and edit permissions otherwise the bundle could not write in your repertory., (*5)
// use JTFileBundle\Form\Type\FileType;
$builder->add('avatar', FileType::class, array(
'data_class' => Avatar::class
));
}
You can use any entity as Avatar for all kinds of doctrine relationships without any problem. When a UploadableFile is persist (or remove) the associate file is upload (or delete)., (*6)
I just added a little feature to download a file from a controller :, (*7)
return $this->get('jt_file.downloader')->createResponse($entity);
And to download a ZIP of several entities :, (*8)
return $this->get('jt_file.downloader')->createResponse($entities);
File handling by doctrine listener
MIT
File handling by doctrine listener
MIT