Luego de instalar todas las dependencias (que son unas cuantas), extender y registrar el bundle tienes que relacionar tus entidades con Sonata\MediaBundle\Entity\Media o Sonata\MediaBundle\Entity\Gallery.
De todas formas te dejo un ejemplo de como podria ser desde cero, va en dos posts porque es medio largo :P, utilizo tu escenario de un post con imagenes, un post va a tener titulo contenido e imagenes, cada imagen va a tener el nombre original de la imagen, fecha de creación y el nombre luego de subir el fichero, la relacion es OneToMany - Bidirectonal, podría ser algo así:
Entities - Post
Código PHP:
Ver originalnamespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\DemoBundle\Entity\Post
*
* @ORM\Table(name="post__post")
* @ORM\Entity
*/
class Post
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $title
*
* @ORM\Column(name="title", type="string", length=255)
*/
protected $title;
/**
* @var string $content
*
* @ORM\Column(name="content", type="text")
*/
protected $content;
/**
* @var ArrayCollection $images
*
* @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\PostImage", mappedBy="post", cascade={"all"})
*/
protected $images;
/**
* Post constructor
*/
public function __construct()
{
$this->images = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set content
*
* @param text $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* Get content
*
* @return text
*/
public function getContent()
{
return $this->content;
}
/**
* Set images
*
* @param array $images
*/
public function setImages($images)
{
foreach($images as $image){
$image->setPost($this);
}
$this->images = $images;
}
/**
* Add image
*
* @param PostImage $image
*/
public function addImage($image)
{
$image->setPost($this);
$this->images[] = $image;
}
/**
* Get images
*
* @return array
*/
public function getImages()
{
return $this->images;
}
}
Entities - PostImage
Código PHP:
Ver originalnamespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Acme\DemoBundle\Entity\PostImage
*
* @ORM\Table(name="post__image")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class PostImage
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* @var string $path
*
* @ORM\Column(name="path", type="string", length=255)
*/
protected $path;
/**
* @var Acme\DemoBundle\Entity\Post $post
*
* @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Post", inversedBy="images")
*/
protected $post;
/**
* @var DateTime $createdAt
*
* @ORM\Column(name="created_at", type="datetime")
*/
protected $createdAt;
/**
* @var Symfony\Component\HttpFoundation\File\UploadedFile $file
*/
public $file;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set post
*
* @param Acme\DemoBundle\Entity\Post $post
*/
public function setPost($post)
{
$this->post = $post;
}
/**
* Get post
*
* @return Acme\DemoBundle\Entity\Post
*/
public function getPost()
{
return $this->post;
}
/**
* Set createdAt
*
* @param DateTime $createdAt
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}
/**
* Get createdAt
*
* @return DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->createdAt = new \DateTime();
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->file) { $this->name = $this->file->getClientOriginalName();
// do whatever you want to generate a unique name
$this->path = uniqid().'.'.$this->file->guessExtension(); }
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->file) { return;
}
// if there is an error when moving the file, an exception will
// be automatically thrown by move(). This will properly prevent
// the entity from being persisted to the database on error
$this->file->move($this->getUploadRootDir(), $this->path);
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if ($file = $this->getAbsolutePath()) {
}
}
/**
* @return mixed null|string
*/
public function getAbsolutePath()
{
return null === $this->path ?: $this->getUploadRootDir() . '/' . $this->path;
}
/**
* @return mixed null|string
*/
public function getWebPath()
{
return null === $this->path ?: $this->getUploadDir() . '/' . $this->path;
}
/**
* @return string
*/
protected function getUploadRootDir()
{
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}
/**
* @return string
*/
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
return 'uploads';
}
}
Ahora los formularios para las entidades:
Forms - Post
Código PHP:
Ver originalnamespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PostType extends AbstractType
{
public function buildForm
(FormBuilder
$builder, array $options) {
$builder
->add('title')
->add('content')
->add('images', 'collection', array( 'type' => new PostImageType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
))
;
}
public function getName()
{
return 'acme_demobundle_posttype';
}
public function getDefaultOptions
(array $options) {
return array('data_class' => 'Acme\DemoBundle\Entity\Post'); }
}
Forms - PostImage
Código PHP:
Ver originalnamespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PostImageType extends AbstractType
{
public function buildForm
(FormBuilder
$builder, array $options) {
$builder->add('file', 'file');
}
public function getName()
{
return 'acme_demobundle_postimagetype';
}
public function getDefaultOptions
(array $options) {
return array('data_class' => 'Acme\DemoBundle\Entity\PostImage'); }
}
to be continued...