2017-02-13 8 views
0

私はSymfonyでnewbyですので、私の無知を許してください。ソナタ管理者投稿データを別のコントローラに渡す方法

私は彼らがタイトルのような一般的なフィールドを持つ親エンティティの「記事」と「ページ」のようなサブentites、「ニュース」などを持って、日付など

私はArticleAdminクラスに記事の初期の形を(作成しました)サブエンティティのタイトルとタイプを選択することが可能で、サブエンティティのAdminクラスを呼び出してPOSTデータを渡そうとしました。しかし、これは動作しませんでした。コントローラのcreateActionはフォームのレンダリングとその処理の両方を担当し、createAction()でオーバーライドしようとするとエラーが発生します。 "エラー:プライベートメソッドSonata \ AdminBundle \親エンティティ

<?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 :: setFormTheme()」ここ

は私のコードです

ArticleAdminコントローラ - 私はCRUDControllerをコピーして(ます$ 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); 
} 
} 

ヘルプ、してください場合はブロック

を置き換えます!たぶんそれは別の方法で実装することができますか?

ありがとうございます!

答えて

0

あなたの$応答を作成する方法は正しいですが、今ではアクションの最後に返す必要があります。そうしないと、転送は行われません。

関連する問題