Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 & SonataMedia: current field not linked to an admin

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!

like image 225
abiyi Avatar asked Aug 18 '12 20:08

abiyi


3 Answers

Just a wild guess, create an admin class for GalleryHasMedia entity.

like image 77
Mun Mun Das Avatar answered Oct 09 '22 12:10

Mun Mun Das


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))
    ;
like image 43
Azam Alvi Avatar answered Oct 09 '22 12:10

Azam Alvi


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 ..

like image 28
Ajeet Varma Avatar answered Oct 09 '22 12:10

Ajeet Varma