I've been tring the last days to make SonataMedia works with Symfony 2.0.16... with no success. Googling around seems like no much people use that bundle or there's a tutorial or an how-to that I'm not aware of, cause I don't get to much info about the error messages I've got so far.
Anyway, my last attempt gave the next error message:
The current field `path` is not linked to an admin. Please create one for the target entity : ``
"path" is the field used to save the file image (relative) path.
AttachmentAdmin.php
<?php
class AttachmentAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add(
'path',
'sonata_type_collection',
array(
'required' => true
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'targetEntity' => 'Application\Sonata\MediaBundle\Entity\GalleryHasMedia',
'link_parameters' => array(
'context' => 'attachment'
)
)
)
->add('notes', 'textarea', array('required' => false))
;
}
// other methods
}
Attachment.php
<?php
class Attachment
{
// other properties
/**
* @var string $path
*
* @ORM\Column(name="path", type="string", nullable=false)
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\GalleryHasMedia", cascade={"persist"})
*/
protected $path;
// other methods
/**
* Set path
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
foreach ($path as $ent) {
$ent->setAttachment($this);
}
}
/**
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
*
* @param \Application\Sonata\MediaBundle\Entity\GalleryHasMedia $path
*/
public function addPath(\Application\Sonata\MediaBundle\Entity\GalleryHasMedia $path)
{
$this->path[] = $path;
}
}
GalleryHasMedia.php
<?php
class GalleryHasMedia extends BaseGalleryHasMedia
{
/**
* @var integer $id
*/
protected $id;
/**
*
* @var File
*/
private $attachment;
/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}
/**
*
* @param \Mercury\CargoRecognitionBundle\Entity\Attachment $attachment
* @return \Application\Sonata\MediaBundle\Entity\GalleryHasMedia
*/
public function setAttachment(\Mercury\CargoRecognitionBundle\Entity\Attachment $attachment = null)
{
$this->attachment = $attachment;
return $this;
}
/**
*
* @return \Application\Sonata\MediaBundle\Entity\File
*/
public function getAttachment()
{
return $this->attachment;
}
}
services.yml
mercury.cargo_recognition.admin.attachment:
class: Mercury\CargoRecognitionBundle\Admin\AttachmentAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: General, label: Attachments }
arguments: [ null, Mercury\CargoRecognitionBundle\Entity\Attachment, "MercuryCargoRecognitionBundle:AttachmentAdmin" ]
Thanks for any info!
Just a wild guess, create an admin class for GalleryHasMedia
entity.
You need to add admin_code like this
$formMapper
->add(
'path',
'sonata_type_collection',
array(
'required' => true
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
'targetEntity' => 'Application\Sonata\MediaBundle\Entity\GalleryHasMedia',
'link_parameters' => array(
'context' => 'attachment'
),
'admin_code' => 'sonata.media.admin.gallery_has_media' // this will be your admin class service name
)
)
->add('notes', 'textarea', array('required' => false))
;
You are trying to apply 'sonata_type_collection' on field 'path' of same entity class 'attachment' , while 'sonata_type_collection' is for collection of embeded form of different class . So you will have to one more entity class suppose 'AttachmentCollection' and in this particular AttachmentsCollection's admin class, you should embed the 'Attachment' class admin .. example:
class AttachmentsCollection extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('attachments', 'sonata_type_collection', array(
'required' => false,
'type_options' => array('delete' => true)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
));
}
}
And also do't forget to do mapping 'one to many' or 'many to many' mapping between 'AttachmentsCollection' and 'Attachments' supposing that one 'AttachmentsCollection' has many 'Attachments' object ..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With