简体   繁体   English

当sonata_type_admin调用时,如何在Sonata的Admin类中获取底层对象?

[英]How to get underlying object in Sonata's Admin class when called by sonata_type_admin?

So, in edit action of Sonata Admin I'm trying to display different form fields depending on create or edit context. 因此,在Sonata Admin的编辑操作中,我试图根据创建编辑上下文显示不同的表单字段。

Maybe some background first.. 也许有些背景先..

I have a Gallery entity and a CoverPhoto entity bound with OneToOne. 我有一个Gallery实体和一个与OneToOne绑定的CoverPhoto实体。

Gallery: 画廊:

/**
 * @ORM\OneToOne(targetEntity="CoverImage", mappedBy="gallery", cascade={"all"}, orphanRemoval=true)
 **/
private $cover;

CoverImage: 封面图片:

/**
 * @ORM\OneToOne(targetEntity="Gallery", inversedBy="cover")
 **/
private $gallery; 

Here's coresponding GalleryAdmin class: 这是相应的GalleryAdmin类:

class GalleriesAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                -> add('name', null, array('label' => 'Nazwa'))
                -> add('category', 'sonata_type_model', array('label' => 'Kategoria'), array('subject' => $this -> getSubject()))
                -> add('cover', 'sonata_type_admin', array('label' => 'Okładka'))
                -> add('images', 'sonata_type_collection', array('by_reference' => false, 'label' => 'Powiązane zdjęcia'), array(
                    'edit' => 'inline',
                    'sortable' => false,
                    'inline' => 'table',
                ))
            ;
    }

    //other stuff 
}

And here we go with CoverImageAdmin : 在这里,我们使用CoverImageAdmin

class CoverImagesAdmin extends Admin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
                -> add('path', 'thumbnail', array('label' => 'Miniatura'))
                -> add('file', 'file', array('required' => false, 'label' => 'Plik'))
        ;
    }
}

"Thumbnail" is my custom form field which displays thumbnail (shocker). “缩略图”是我的自定义表单字段,显示缩略图(shocker)。 But now i want this field only appear in "edit" context. 但现在我希望这个字段只出现在“编辑”上下文中。

This should be piece of cake with 这应该是小菜一碟

$this -> getSubject()

method of Admin class and condition. Admin类和条件的方法。 Unfortunately when I call getSubject() in CoverImagesAdmin class which is used to render nested form it always returns null . 不幸的是,当我在CoverImagesAdmin类中调用getSubject()时,它用于呈现嵌套形式,它总是返回null The same with getParent(); 与getParent()相同;

Calling getForm() results in 调用getForm()会导致

Fatal error: Maximum function nesting level of '500' reached, aborting! 致命错误:达到'500'的最大功能嵌套级别,正在中止! in /home/flameheart/Projects/KolberPhotography/vendor/symfony/symfony/src/Symfony/Component /OptionsResolver/Options.php on line 350 在/ home / flameheart / Projects / KolberPhotography / vendor / symfony / symfony / src / Symfony / Component /OptionsResolver/Options.php 350行

I've tried to call about every method of Admin and FormMapper just to determine form's context but ended up with nothing. 我试图调用Admin和FormMapper的每个方法来确定表单的上下文但最终没有任何结果。

Do you guys have any idea how to solve this in a clean way ? 你们有什么想法以干净的方式解决这个问题吗?

I managed to do it this way: 我设法这样做:

protected function configureFormFields(FormMapper $formMapper)
{

    if($this -> getRoot() -> getSubject() -> getCover() && $this -> getRoot() -> getSubject() -> getCover() -> getId() )
    {
        $formMapper -> add('path', 'thumbnail', array('label' => 'Miniatura', 'attr' => array('id' => 'gallery_cover_image'), 'label_attr' => array('id' => 'gallery_cover_label')));
    }

    $formMapper -> add('file', 'file', array('required' => false, 'label' => 'Plik'));
}

Imo, this Sonata thing really needs loads of documentation and refactoring instead of further development. Imo,这个Sonata的东西真的需要大量的文档和重构而不是进一步的开发。

It's an old question, I know, but the cleanest way I have found of doing this is: 我知道这是一个老问题,但我发现这样做最干净的方法是:

$this->id($this->getSubject())

If it returns true it is an edit form, if it is false, it is a create form. 如果返回true,则为编辑表单,如果为false,则为创建表单。

You can get the subject inside of an admin with $this->subjectExists() and you can check the context of the configureFormFields() admin function with a function $this->subjectExists() . 您可以使用$this->subjectExists()获取管理员内部的主题,并且可以使用函数$this->subjectExists()检查configureFormFields()管理函数的上下文。 If it's true, you're editing, otherwise you're creating! 如果这是真的,那么你正在编辑,否则你就是在创作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM