2

我有一个问题来建立一个表格之间有很多关系的表格。您可以在下图中看到关系结构:表之间的关系

我在 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 为空(但它应该从产品中获取)。

另外我想做的是这样的: 在此处输入图像描述

还有一些来自 Admin 目录的代码:

###### 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()
    ;
4

1 回答 1

1

对于第一部分,在你的Product实体中,你有addProductCombinations方法。让它像这样:

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

这应该让你动起来。或者,您可以在管理员中设置产品内部prePersist/preUpdate操作。

问题的第二部分有点不清楚。你想拥有不同类型的属性,比如文本/选择/多项选择吗?如果是这样,您将不得不使用如何使用表单事件动态修改表单来动态修改表单。但是,嘿,也许您可​​以重用一些现有的捆绑包,例如packagist: assortment

于 2013-08-27T19:01:52.217 回答