【问题标题】:Symfony2 & SonataMedia: current field not linked to an adminSymfony2 和 SonataMedia:当前字段未链接到管理员
【发布时间】:2012-08-18 20:25:20
【问题描述】:

我最近几天一直在努力让 SonataMedia 与 Symfony 2.0.16 一起工作......但没有成功。谷歌搜索似乎没有多少人使用该捆绑包,或者有一个我不知道的教程或操作方法,因为到目前为止我没有得到关于错误消息的太多信息。

无论如何,我最后一次尝试给出了下一条错误消息:

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

“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" ]

感谢您提供任何信息!

【问题讨论】:

    标签: php symfony symfony-sonata


    【解决方案1】:

    只是一个疯狂的猜测,为GalleryHasMedia 实体创建一个管理类。

    【讨论】:

    • 我从未见过 GalleryHasMediaAdmin 文件,但我会尝试一下 ;-)
    • 弄清楚了吗?同样的问题。
    • 这就是答案。您需要创建 GalleryHasMediaAdmin,然后将其添加到 services.yml
    【解决方案2】:

    你需要像这样添加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))
        ;
    

    【讨论】:

      【解决方案3】:

      您正在尝试将“sonata_type_collection”应用于同一实体类的字段“路径” > 'attachment' ,而 'sonata_type_collection' 用于收集不同类的嵌入形式。 因此,您将不得不再添加一个实体类,假设为“AttachmentCollection”,并且在这个特定的 AttachmentsCollection's 管理类中,您应该嵌入“Attachment' 班级管理员 .. 示例:

      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' 和'Attachments' 假设一个 'AttachmentsCollection' 有许多 'Attachments' 对象..

      【讨论】:

      • 请记住,该问题是在 2012 年 9 月发布的。不是当前版本的 SonataMedia 也不是 Symfony2。一年前我为了 VichUploaderBundle 放弃了 SonataMediaBundle(更简单)。
      【解决方案4】:

      看起来您需要做的是将admin_code 传递给add() 方法的$fieldDescriptionOptions 参数中的管理部分的名称 (mercury.cargo_recognition.admin.attachment)。

      【讨论】:

      • 我很久以前就解决了这个问题,但我不记得出了什么问题以及我做了什么来解决它。我现在正在使用 vichUploaderBundler(它非常适合我的数据库结构)。
      【解决方案5】:

      尽管这个问题已经存在,但我仍会添加一些关于如何解决此问题的内容,以帮助其他可能遇到此问题的人。根本问题是 $path targetEntity("Application\Sonata\MediaBundle\Entity\GalleryHasMedia") 没有管理类。

      要解决它,请为您的 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" ]
      

      【讨论】:

      • 与此特定问题无关,如果您的属性映射到 targetEntity,则此解决方案成立。确保您首先设置映射(如果您使用的是学说 ODM,比如学说_phpcr,则映射使用类似的东西完成:@PHPCR\ReferenceMany(targetDocument="Acme\DemoBundle\Document\TheTargetClass", cascade={"persist" })
      【解决方案6】:

      如果您的 GalleryHasMedia 管理类尚未创建,并且您尚未将服务代码放入 config.yml 文件中,请先执行此操作,然后重试。我遇到了同样的问题并通过这种方式得到了解决。

      【讨论】:

        【解决方案7】:

        五年后,值得注意的是,“目标实体”本身在错误文本中似乎是空白的。我们得到一组反引号,而不是实体名称。

        (我正在开发一个旧应用程序,现在遇到了类似的错误。我为我的实体生成了一个管理类并将其注册在正确的位置,看起来该应用程序无法确定我的目标entity is -- 默认情况下它会阻止它找到相关的管理类。我有点担心这个问题,明天可能会悬赏。)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-07-22
          • 1970-01-01
          • 1970-01-01
          • 2010-10-06
          • 1970-01-01
          • 2022-11-08
          • 1970-01-01
          相关资源
          最近更新 更多