索纳塔管理员。如何将后期数据传递给另一个控制器

时间:2017-02-13 17:48:42

标签: symfony crud sonata-admin

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

我有一个父母实体“文章”和子网站,如“页面”,“新闻”等。他们有共同的领域,如标题,日期等。

我为Article创建了初始表单(在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-&gt; 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);
}
}

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

提前致谢!!

2 个答案:

答案 0 :(得分:0)

你做出$响应的方式是正确的,但现在你需要在行动结束时返回它,否则前进将永远不会发生

答案 1 :(得分:0)

关于你得到的错误,

相反:

$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());