我是Symfony的新手,所以请原谅我的无知。
我有一个父实体"文章"和子条目,如"页面"、"新闻"等。它们具有标题,日期等公共字段。
我为文章创建了初始表单(在 ArticleAdmin 类中(,可以选择子实体的标题和类型,并尝试调用子实体的 Admin 类并将 POST 数据传递给那里。但这不起作用,因为控制器中的createAction负责表单渲染及其处理,当我尝试在我的createAction((中覆盖它时,我收到错误"错误:调用私有方法Sonata\AdminBundle\Controller\CRUDController::setFormTheme((">
这是我的代码:
文章管理员 - 父实体
<?php
namespace A26CMSContentBundleAdmin;
use SonataAdminBundleAdminAbstractAdmin;
use SonataAdminBundleShowShowMapper;
use SonataAdminBundleFormFormMapper;
use SonataAdminBundleDatagridListMapper;
use SonataAdminBundleDatagridDatagridMapper;
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');
}
}
?>
页面管理员 - 子实体
<?php
namespace A26CMSPagesBundleAdmin;
use SonataAdminBundleAdminAbstractAdmin;
use SonataAdminBundleShowShowMapper;
use SonataAdminBundleFormFormMapper;
use SonataAdminBundleDatagridListMapper;
use SonataAdminBundleDatagridDatagridMapper;
use IvoryCKEditorBundleFormTypeCKEditorType;
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');
}
}
?>
文章管理员控制器 - 我复制了 CRUDController 并替换了块
if ($form->isSubmitted((( {...}
<?php
namespace A26CMSContentBundleController;
use SonataAdminBundleControllerCRUDController 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 SymfonyComponentFormForm */
$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);
}
}
请帮忙!也许可以用另一种方式实现?
提前感谢!!
你做$response的方式是正确的,但现在你需要在操作结束时返回它,否则转发将永远不会发生
关于你得到的错误,
相反:
$this->setFormTheme($formView, $this->admin->getFormTheme());
用:
$this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFormTheme());
或:
$this->get('twig')->getExtension('SymfonyBridgeTwigExtensionFormExtension')->renderer->setTheme($formView, $this->admin->getFormTheme());