2017 © Pedro Peláez
 

symfony-bundle file-fileentitybundle

image

brainx2/file-fileentitybundle

  • Tuesday, January 27, 2015
  • by brainx2
  • Repository
  • 2 Watchers
  • 0 Stars
  • 1,143 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

FileEntityBundle

Simple way to upload files, (*1)

Installation:

Pretty simple with Composer, add:, (*2)

{
    "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="" />

The Versions

27/01 2015

dev-master

9999999-dev

  Sources   Download

MIT

by joker

file upload file entity