brainx2/file-fileentitybundle

Installs: 1 179

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 2

Open Issues: 0

Type:symfony-bundle

dev-master / 1.0.0.x-dev 2015-01-27 18:29 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:38:55 UTC


README

Simple way to upload files

Installation:

Pretty simple with Composer, add:

{
    "require": {
        "brainx2/file-fileentitybundle": "1.0.0.*@dev"
    }
}

Add FileEntityBundle to your application kernel

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Brainx2\File\FileEntityBundle\Brainx2FileFileEntityBundle(),
        // ...
    );
}

Usage examples:

ORM.yml

Demo\BackendBundle\Entity\Demo:
    type:  entity
    table: demo
    id:
        id:
            type: integer
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 50
        image_first:
            type: string
            length: 255
            nullable: true
        image_second:
            type: string
            length: 255
            nullable: true
            
    lifecycleCallbacks:
        prePersist: [upload]
        preUpdate: [upload]
        preRemove: [remove]

Entity

namespace Demo\BackendBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Brainx2\File\FileEntityBundle\Entity\FileEntity as FileEntity;


/**
 * Demo
 */
class Demo extends FileEntity
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    protected $image_first;

    /**
     * @var string
     */
    protected $image_second;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Demo
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set image_first
     *
     * @param string $imageFirst
     * @return Demo
     */
    public function setImageFirst($imageFirst)
    {
        if(!is_null($imageFirst)) {
            $this->setFiles('image_first');

            $this->image_first = $imageFirst;
        }

        return $this;
    }

    /**
     * Get image_first
     *
     * @return string 
     */
    public function getImageFirst()
    {
        return $this->image_first;
    }

    /**
     * Set image_second
     *
     * @param string $imageSecond
     * @return Demo
     */
    public function setImageSecond($imageSecond)
    {
        if(!is_null($imageSecond)) {
            $this->setFiles('image_second');

            $this->image_second = $imageSecond;
        }

        return $this;
    }

    /**
     * Get image_second
     *
     * @return string 
     */
    public function getImageSecond()
    {
        return $this->image_second;
    }
    
    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function upload()
    {
        return parent::upload();
    }
    
    /**
     * @ORM\PreRemove
     */
    public function remove()
    {
        $this->setFiles('image_first');
        $this->setFiles('image_second');
        
        return parent::remove();
    }    
    
    protected function getUploadRootDir()
    {
        return __DIR__.'/../../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        return '/uploads/files';
    } 
}

Form

class DemoType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('image_first', 'file', array(
                'required' => false, 
                'data_class' => null
            ))
            ->add('image_second', 'file', array(
                'required' => false, 
                'data_class' => null
            ))
        ;
    }

    //.......
}

View

<img src="{{ entity.webpath('image_first') }}" alt="" />
<img src="{{ entity.webpath('image_second') }}" alt="" />