Skip to content

Instantly share code, notes, and snippets.

@garak
Created February 16, 2017 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garak/9c7240a05d6fbe1f30b674c6bc31bec3 to your computer and use it in GitHub Desktop.
Save garak/9c7240a05d6fbe1f30b674c6bc31bec3 to your computer and use it in GitHub Desktop.
uploadable with embedded
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Embeddable
* @Vich\Uploadable
*/
class Foo
{
/**
* @Vich\UploadableField(mapping="image", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column()
*
* @var string
*/
private $imageName;
/**
* @var \DateTime
*
* @ORM\Column(name="updated", type="datetime", nullable=true)
*/
private $updated;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File $image
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image instanceof UploadedFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updated = new \DateTimeImmutable();
}
}
/**
* @return File|null
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $imageName
*
*/
public function setImageName($imageName)
{
$this->imageName = $imageName;
}
/**
* @return string|null
*/
public function getImageName()
{
return $this->imageName;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
* @Vich\Uploadable
*/
class User
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var Foo
*
* @ORM\Embedded(class="AppBundle\Entity\Foo")
*/
private $foo;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param Foo $foo
*/
public function setFoo(Foo $foo)
{
$this->foo = $foo;
}
/**
* @return Foo
*/
public function getFoo()
{
return $this->foo;
}
/**
* @param string $imageName
*
*/
public function setImageName($imageName)
{
$this->foo->setImageName($imageName);
}
/**
* @return string|null
*/
public function getImageName()
{
return $this->foo->getImageName();
}
/**
* @param File $image
*/
public function setImageFile(File $image = null)
{
$this->foo->setImageFile($image);
}
/**
* @return File|null
*/
public function getImageFile()
{
return $this->foo->getImageFile();
}
}
@welcoMattic
Copy link

This example, doesn't work at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment