2013-08-26 1 views
2

У меня возникла проблема в создании форм со многими отношениями между таблицами. Структура отношений вы можете увидеть на рисунке ниже: relations between tablesпостроить форму для таблиц со многими отношениями

Мои отношения в YML:

######### Products.orm.yml ######### 

    type: entity 
    table: products 
    fields: 
    ... 
    lifecycleCallbacks: { } 
    oneToMany: 
    productCombinations: 
     targetEntity: ProductsCombinations 
     mappedBy: product 

######### ProductsCombinations.orm.yml ######### 

    type: entity 
    table: products_combinations 
    fields: 
    ... 
    lifecycleCallbacks: { } 
    oneToMany: 
     productAttributes: 
     targetEntity: ProductsAttributesJoin 
     mappedBy: productCombinations 
    manyToOne: 
     product: 
     targetEntity: Products 
     inversedBy: productCombinations 
     joinColumn: 
      name: product_id 
      referencedColumnName: id 

######### ProductsAttributesJoin.orm.yml ######## 

type: entity 
table: null 
fields: 
    combinationID: 
     type: bigint 
     column: combination_id 
     id: true 
     generator: 
      strategy: NONE 
    attributeID: 
     type: bigint 
     id: true 
     generator: 
      strategy: NONE 
     column: attribute_id 
lifecycleCallbacks: { } 
manyToOne: 
    productCombinations: 
     targetEntity: ProductsCombinations 
     inversedBy: productAttributes 
     joinColumn: 
     name: combination_id 
     referencedColumnName: combination_id 
    attributes: 
     targetEntity: Attributes 
     inversedBy: productAttributesJoin 
     joinColumn: 
     name: attribute_id 
     referencedColumnName: id 

######### Attributes.orm.yml ######### 

    type: entity 
    table: products_attributes 
    fields: 
    ... 
    lifecycleCallbacks: { } 
    oneToMany: 
     productAttributesJoin: 
     targetEntity: ProductsAttributesJoin 
     mappedBy: attributes 
    manyToOne: 
     productAttributesDefinitions: 
     targetEntity: AttributesDefinitions 
     inversedBy: productAttributes 
     joinColumn: 
      name: attribute_id 
      referencedColumnName: id 

######### AttributesDefinitions.orm.yml ######### 

    type: entity 
    table: products_attributes_definitions 
    fields: 
    ... 
    lifecycleCallbacks: { } 
    oneToMany: 
     productAttributes: 
     targetEntity: Attributes 
     mappedBy: productAttributesDefinitions 
    manyToOne: 
     productAttributesDefinitions: 
     targetEntity: AttributesDefinitions 
     inversedBy: productAttributes 
     joinColumn: 
      name: attribute_id 
      referencedColumnName: id 

То, что я получил знать, что: epos_wrong

Что не работает, когда я буду нажмите «Добавить новое», затем я получил всплывающее окно с полями ProductsCombinations, но когда я их заполнил и нажмите «Сохранить», появится сообщение об ошибке, что product_id имеет значение NULL (но оно должно быть взято из «Продуктов»).

Кроме того, что я хотел бы сделать что-то вроде этого: enter image description here

Также немного кода из каталога администратора:

###### ProductsView.php ###### 

$formMapper->with('Attributes') 
      ->add('productCombinations', 'sonata_type_collection') 
     ->end() 

###### ProductsCombinations.php ###### 

    $formMapper 
     ->with('General') 
      ->(some main fields from productCombinations) 
      ->add('productAttributes', 'sonata_type_collection', array(), array(
      'edit' => 'inline', 
      'inline' => 'table', 
      'sortable' => 'position' 
     )) 

###### ProductsAttributesJoin.php ###### 

    $formMapper 
     ->with('General') 
      ->add('attributes', 'sonata_type_model') 
     ->end() 
    ; 

ответ

1

Для первой части, в вас Product сущности, у вас есть addProductCombinations способ. Сделайте это так:

public function addProductCombinations($object) 
{ 
    $this->productCombinations[] = $object; 
    $object->setProduct($this); // This line is important. 
} 

Это должно заставить вас двигаться. Кроме того, вы можете установить продукт внутри prePersist/preUpdate действий в вашем администраторе.

Вторая часть вопроса немного неясна. Вы хотите иметь атрибуты разных типов, такие как text/choice/multiple choice? Если это так, вам придется динамически изменять форму, используя How to Dynamically Modify Forms Using Form Events. Но эй, может быть, вы можете повторно использовать некоторые существующие пакеты, например packagist: assortment.