Symfony2 & SonataMedia: current field not associated with an administrator

I tried the last days for SonataMedia to work with Symfony 2.0.16 ... without success. It looks like a Google search looks like many people are not using this package or there is no tutorial or practical guide that I don’t know about, because I don’t get much information about the error messages that I have.

Anyway, my last attempt gave the following error message:

The current field `path` is not linked to an admin. Please create one for the target entity : `` 

"path" is a field used to save the file path of a (relative) file.

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!

+5
source share
7 answers

, GalleryHasMedia.

+3

admin_code

$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))
    ;
+2

" sonata_type_collection" " " " ', ' sonata_type_collection ' . , , AttachmentCollection ' AttachmentsCollection admin, admin..  :

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',
                    ));
     }
  }

" " " " " AttachmentsCollection" ' ', , AttachmentsCollection ' .

+1

, admin_code, admin (mercury.cargo_recognition.admin.attachment) $fieldDescriptionOptions add().

0

, - , , , . , $path targetEntity ( "\\MediaBundle\Entity\GalleryHasMedia" ) .

, admin targetEntity:

class GalleryHasMediaAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('attachment', 'file')
        ;
    }

    // other methods
}

services.yml

mercury.cargo_recognition.admin.galleryhadmedia:
    class: Mercury\CargoRecognitionBundle\Admin\GalleryHasMediaAdmin
    tags:
        - { name: sonata.admin, manager_type: orm, group: General, label: 'Gallery Has Media' }
    arguments: [ null, Mercury\CargoRecognitionBundle\Entity\GalleryHasMedia, "MercuryCargoRecognitionBundle:GalleryHasMediaAdmin" ]
0

GalleryHasMedia , config.yml, . , .

0

Five years later, it is worth noting that the “target entity” itself seems empty in the error text. We get a set of backlinks, not the name of the object.

(I’m working on an old application and getting a similar error right now. I created an admin class for my object and registered it in the right place, and it looks like the application is having problems figuring out if my target is an entity - which by default prevents it from finding a related administrator class. I kind of ripped my hair over it and probably generosity will pounce on him tomorrow.)

0
source

All Articles