Skip to content

Instantly share code, notes, and snippets.

@roniemicro
Last active August 29, 2015 14:20
Show Gist options
  • Save roniemicro/8e87914a576cf85b151b to your computer and use it in GitHub Desktop.
Save roniemicro/8e87914a576cf85b151b to your computer and use it in GitHub Desktop.
Doctrine Uploadable extention uses
...
new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle(),
...
....
"stof/doctrine-extensions-bundle": "~1.1@dev",
.....
stof_doctrine_extensions:
default_locale: en_US
uploadable: ~
class:
uploadable: ...\UploadableListener
orm:
default:
uploadable: true
$em->persist($entity);
.....
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
$uploadableManager->markEntityToUpload($entity, $entity->getFile());
....
$em->flush();
<?php
namespace Setting\Bundle\ContentBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Entity
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Setting\Bundle\ContentBundle\Entity\SiteSliderRepository")
* @Gedmo\Uploadable(pathMethod="getUploadRootDir", allowOverwrite=false, appendNumber=true)
*/
class Entity
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
* @Gedmo\UploadableFileName
* @ORM\Column(name="path", type="string", length=255, nullable=true)
*/
private $path;
/**
* @Assert\File(maxSize="")
*/
protected $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
protected function getUploadRootDir()
{
return "file_path";
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param int $id
*/
public function setId($id)
{
$this->id = $id;
}
}
<?php
namespace ....;
use Gedmo\Uploadable\UploadableListener as BaseListener,
Gedmo\Uploadable\FileInfo\FileInfoInterface;
class UploadableListener extends BaseListener
{
public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorClass = false, $overwrite = false, $appendNumber = false, $object)
{
$info = parent::moveFile($fileInfo, $path, $filenameGeneratorClass, $overwrite, $appendNumber, $object);
$info['fileName'] = basename($info['filePath']);
return $info;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment