Symfony2 & SonataMedia: current field not linked to an admin
Asked Answered
T

7

8

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!

Tinsmith answered 18/8, 2012 at 20:25 Comment(0)
S
5

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

Salicaceous answered 18/8, 2012 at 23:40 Comment(3)
I've never seen that a GalleryHasMediaAdmin file, but I'm gonna give it a wild try ;-)Tinsmith
Figure it out yet? Same issue.Somatoplasm
This is the answer. You needed to created GalleryHasMediaAdmin and then add it to services.ymlBarfly
R
3

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))
    ;
Rifleman answered 18/7, 2014 at 6:43 Comment(0)
H
1

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

Harday answered 18/7, 2014 at 7:56 Comment(1)
Remember that the issue was posted on September 2012. Not the current version of SonataMedia nor Symfony2. I drop SonataMediaBundle a year ago for VichUploaderBundle (which is simpler).Tinsmith
S
0

Looks like what you need to do is pass in an admin_code which is the name of the admin section (mercury.cargo_recognition.admin.attachment) in the $fieldDescriptionOptions parameter to the add() method.

Somatoplasm answered 16/10, 2013 at 0:44 Comment(1)
I solved the issue long time ago, but I don't remember what was wrong and what I did to fix it. I'm using vichUploaderBundler right now (which suits perfectly with my database structure).Tinsmith
B
0

As old as this issue is, I'll still add something on how this can be resolved to help anyone else who might run into it. The root issue is that the $path targetEntity("Application\Sonata\MediaBundle\Entity\GalleryHasMedia") doesn't have an admin class.

To resolve it, add an admin class for your targetEntity:

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

    // other methods
}

Then add that admin class to 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" ]
Barfly answered 30/11, 2015 at 11:5 Comment(1)
Unrelated to this particular question, this solution holds if your property is mapped to a targetEntity. Make sure you set-up mapping first (If you are using doctrine ODM, like doctrine_phpcr, the mapping is done using something like: @PHPCR\ReferenceMany(targetDocument="Acme\DemoBundle\Document\TheTargetClass", cascade={"persist"})Barfly
T
0

if your admin class for GalleryHasMedia is not yet created and you have not put the service code in your config.yml file then first do that and try again. I had the same issue and got resolved this way.

Truax answered 31/1, 2018 at 10:34 Comment(0)
G
0

Five years later, it's worth noting that the "target entity" itself appears to be blank in the error text. We get a set of backticks, rather than an entity name.

(I am working on an old app and getting a similar error right now. I have generated an admin class for my entity and registered it in the correct place, and it looks like the app is having trouble figuring out what my target entity is -- which by default prevents it from finding the related admin class. I'm sort of tearing my hair out over this one and will probably put up a bounty tomorrow on it.)

Gowan answered 13/2, 2018 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.