我在构建表之间有很多关系的表单时遇到了问题。您可以在下面的图片中看到关系结构:
我在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
我所知道的是:
什么不起作用,当我点击Add new然后我得到了来自ProductsCombinations的字段的弹出窗口,但是当我填写它们并按Save然后它显示错误,product_id为null(但它应该从Products中取出)
我想做的事情也是这样的:
还有一些来自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()
;
答案 0 :(得分:1)
对于第一部分,在您Product
实体中,您拥有addProductCombinations
方法。这样做:
public function addProductCombinations($object)
{
$this->productCombinations[] = $object;
$object->setProduct($this); // This line is important.
}
这会让你感动。或者,您可以在管理员的prePersist
/ preUpdate
操作中设置产品。
问题的第二部分有点不清楚。您是否希望拥有不同类型的属性,如文本/选择/多项选择?如果是这样,您将必须使用How to Dynamically Modify Forms Using Form Events动态修改表单。但是,嘿,也许你可以重用一些现有的包,比如packagist: assortment。