Question

I have got a problem to build a forms with many relations between tables. The structure of relations you can see on below picture:relations between tables

My relations in 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

What I have got know is that: epos_wrong

What is not working as when I will click Add new then I got popup with fields from ProductsCombinations but when I will fill them in and press Save then it is showing error that product_id is null (but it should take it from Products).

Also what I would like to do is something like that: enter image description here

Also a bit of code from Admin directory:

###### 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()
    ;
Was it helpful?

Solution

For the first part, in you Product entity, you have addProductCombinations method. Make it like so:

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

This should get you moving. Alternatively, you can set product inside prePersist / preUpdate actions in your admin.

The second part of the question is a bit unclear. Do you want to have attributes that are of different types like text / choice / multiple choice? If so, you will have to dynamically modify the form using How to Dynamically Modify Forms Using Form Events. But hey, maybe you can reuse some existing bundles, like packagist: assortment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top