2012-08-27 27 views
6

Ví dụ tôi có 3 đơn vị:Sonata quản Bundle Loại Collection Customization

  • Thể loại
  • mục con
  • sản phẩm

Trong SonataAdminBundle Tôi muốn để có thể thêm trong khi mục con chỉnh sửa Danh mục và Sản phẩm trong khi chỉnh sửa Danh mục phụ.

Theo ý tưởng này tôi đã tạo các trường, nhưng SonataAdminBundle bắt đầu phát "Khởi động" với chúng.

Khi tôi mở Danh mục tôi thấy các danh mục con có liên quan chứa các sản phẩm liên quan.

Tôi làm cách nào để cắt trường "Sản phẩm" trong trường hợp này?

Cập nhật:

lớp của tôi (giản thể) trông như thế này:

// .../CoreBundle/Admin/CategoryAdmin.php 
protected function configureFormFields(FormMapper $formMapper) { 
    $formMapper 
    ->add('name', null, array('required' => true)) 
    ->add('url', null, array('required' => true)) 
    ->add('subcategories', 'sonata_type_collection', array('by_reference' => true),  array(
    'edit' => 'inline', 
    'sortable' => 'pos', 
    'inline' => 'table',)); 
} 


// .../CoreBundle/Admin/SubcategoriesAdmin.php 
protected function configureFormFields(FormMapper $formMapper) 
{ 
    $formMapper 
      ->add('name', null, array('label' => 'name')) 
      ->add('category_id', null, array('label' => 'Category')) 
      ->add('url', null, array('label' => 'Url')) 
      ->add('products', 'sonata_type_collection', 
        array('by_reference' => false), 
        array(
         'edit' => 'inline', 
         'sortable' => 'pos', 
         'inline' => 'table', 
       )); 
} 

// .../CoreBundle/Admin/ProductsAdmin.php 
protected function configureFormFields(FormMapper $formMapper) { 
    $formMapper 
      ->add('name', null, array('label' => 'Заголовок')) 
      ->add('subcategory_id', null, array('label' => 'Subcategory')); 
} 

Schema trông như thế này: enter image description here Và trong AdminBundle nó trông như thế này: enter image description here

Trả lời

5

Tại sao bạn không thử điều gì đó dọc theo các dòng sau:

// .../CoreBundle/Admin/SubcategoriesAdmin.php 
protected function configureFormFields(FormMapper $formMapper) 
{ 
    $formMapper 
      ->add('name', null, array('label' => 'name')) 
      ->add('category_id', null, array('label' => 'Category')) 
      ->add('url', null, array('label' => 'Url')); 

    // only show the child form if this is not itself a child form 
    if (!$formMapper->getFormBuilder()->getForm()->hasParent()) { 
     $formmapper 
      ->add('products', 'sonata_type_collection', 
        array('by_reference' => false), 
        array(
         'edit' => 'inline', 
         'sortable' => 'pos', 
         'inline' => 'table', 
       )); 
    } 
} 
1

Giải pháp được đưa ra bởi @likeitlikeit không hoạt động đối với symfony2.0.

Bằng cách nào đó, hasParent() luôn trả về false.

Là một workaround:

if (!is_numeric($formMapper->getFormBuilder()->getForm()->getName())) {} 

Tên bằng một bộ sưu tập sẽ có dạng số (0, 1, 2, ...) trong khi ở một solo thành nó sẽ là một băm.