0
我是Symfony的newby,所以請原諒我的無知。索納塔管理員。如何將帖子數據傳遞給另一個控制器
我有一個父實體「條」和子的entites像「頁」,「新聞」等,他們有一個像標題共通領域,日期等
我創建初始形式條(在ArticleAdmin類)哪裏可以選擇標題和類型的子實體,並試圖調用子實體的Admin類並傳遞POST數據。但這不起作用,因爲控制器中的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和更換塊
如果($形式 - > 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);
}
}
幫助,請!也許它可以以另一種方式實現?
在此先感謝!