奏鸣曲管理员。如何将发布数据传递给另一个控制器

Posted

技术标签:

【中文标题】奏鸣曲管理员。如何将发布数据传递给另一个控制器【英文标题】:Sonata admin. How to pass post data to another controller 【发布时间】:2017-07-01 19:21:47 【问题描述】:

我是 Symfony 的新手,请原谅我的无知。

我有一个父实体“文章”和子实体,如“页面”、“新闻”等。它们有共同的字段,如标题、日期等。

我为文章创建了初始表单(在 ArticleAdmin 类中),可以在其中选择子实体的标题和类型,并尝试调用子实体的 Admin 类并在那里传递 POST 数据。但这不起作用,因为 Controller 中的 createAction 负责表单呈现及其处理,当我尝试在 createAction() 中覆盖它时,我收到错误“错误:调用私有方法 Sonata\AdminBundle\控制器\CRUDController::setFormTheme()"

这是我的代码:

ArticleAdmin - 父实体

<?php

namespace A26\CMS\ContentBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;

class ArticleAdmin extends AbstractAdmin


// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)

    $formMapper
        ->add('title', 'text', array(
            'label' => 'Title'
        ))
        ->add('slug', 'text', array(
            'label' => 'Slug'
        ))
        ->add('type', 'choice', array(
            'choices' => array(
                'Page'      => 'Text Page',
                'News'      => 'News'
            ),
        ));


protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    $datagridMapper
        ->add('title');


protected function configureListFields(ListMapper $listMapper)

    $listMapper
        ->addIdentifier('title')
        ->add('is_publish');


protected function configureShowFields(ShowMapper $showMapper)

    $showMapper
        ->add('title');


?>

PageAdmin - 子实体

<?php

namespace A26\CMS\PagesBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Ivory\CKEditorBundle\Form\Type\CKEditorType;

class PageAdmin extends AbstractAdmin


// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)

    $formMapper
        ->tab('Content')
            ->with('Content')
                ->add('title', 'text', array(
                    'label' => 'Title'
                ))
                ->add('content', CKEditorType::class, array(
                    'label'         => 'Content'
                ))
            ->end()
        ->end();


// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)

    $datagridMapper
        ->add('title');


// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)

    $listMapper
        ->addIdentifier('title');


// Fields to be shown on show action
protected function configureShowFields(ShowMapper $showMapper)

    $showMapper
        ->add('title');


?>

ArticleAdmin 控制器 - 我复制了 CRUDController 并替换了块

if ($form->isSubmitted()) ...

<?php

namespace A26\CMS\ContentBundle\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;

class ArticleAdminController extends Controller


public function createAction()

    $request = $this->getRequest();
    // the key used to lookup the template
    $templateKey = 'edit';

    $this->admin->checkAccess('create');

    $class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());

    if ($class->isAbstract()) 
        return $this->render(
            'SonataAdminBundle:CRUD:select_subclass.html.twig',
            array(
                'base_template' => $this->getBaseTemplate(),
                'admin' => $this->admin,
                'action' => 'create',
            ),
            null,
            $request
        );
    

    $object = $this->admin->getNewInstance();

    $preResponse = $this->preCreate($request, $object);
    if ($preResponse !== null) 
        return $preResponse;
    

    $this->admin->setSubject($object);

    /** @var $form \Symfony\Component\Form\Form */
    $form = $this->admin->getForm();
    $form->setData($object);
    $form->handleRequest($request);

    if ($form->isSubmitted()) 
        $response = $this->forward("A26CMSPagesBundle:PageAdmin:create", array('_sonata_admin' => $this->container->get('request')->get('_sonata_admin')));
        dump($response);
        die();
    

    $formView = $form->createView();
    // set the theme for the current Admin Form
    $this->setFormTheme($formView, $this->admin->getFormTheme());

    return $this->render($this->admin->getTemplate($templateKey), array(
        'action' => 'create',
        'form' => $formView,
        'object' => $object,
    ), null);


请帮忙!也许它可以用另一种方式实现?

提前致谢!!

【问题讨论】:

【参考方案1】:

您的 $response 方式是正确的,但现在您需要在操作结束时返回它,否则转发将永远不会发生

【讨论】:

【参考方案2】:

关于你得到的错误,

改为:

$this->setFormTheme($formView, $this->admin->getFormTheme());

使用:

$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFormTheme());

或者:

$this->get('twig')->getExtension('Symfony\Bridge\Twig\Extension\FormExtension')->renderer->setTheme($formView, $this->admin->getFormTheme());

【讨论】:

以上是关于奏鸣曲管理员。如何将发布数据传递给另一个控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据传递给其他视图? [关闭]

如何在将数据推送到导航堆栈之前将数据传递给视图控制器并加载它?

如何将集合视图数据传递给新的视图控制器

无法将 UITableViewCell didSelectRowAtIndexPath 数据传递给另一个视图控制器

如何以编程方式在视图控制器之间传递数据?我想将我的UITextfield数据传递给另一个视图控制器[重复]

将 tableViewCell 中的数据传递给另一个 VC 失败