2017 © Pedro Peláez
 

bundle file-bundle

File handling by doctrine listener

image

jimmytournemaine/file-bundle

File handling by doctrine listener

  • Friday, September 9, 2016
  • by jimtournem
  • Repository
  • 1 Watchers
  • 0 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Installation

Step 1: Download the Bundle

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)

Step 2: Enable the Bundle

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(),
        );

        // ...
    }

    // ...
}

How to use it ?

Create your entity

<?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)

Create the form

// use JTFileBundle\Form\Type\FileType;
$builder->add('avatar', FileType::class, array(
        'data_class' => Avatar::class
    ));
}

That's it !

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)

Download your files

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);

The Versions

09/09 2016

dev-master

9999999-dev

File handling by doctrine listener

  Sources   Download

MIT

The Requires

 

03/09 2016

1.0.0.x-dev

1.0.0.9999999-dev

File handling by doctrine listener

  Sources   Download

MIT

The Requires