How To Embed EAV Model In Symfony2 Collection Form

Today we are going to embed EAV(Entity Attribute Value) model using Symfony2 Collection Form component. Collection Form in Symfony2 means that a specific form field needs to be used to take multiple values by duplicating the field as many times as needed.

The above explained feature of Symfony2 was used to create dynamic generation & removal of specific form fields through collections for one of our client’s projection. In Symfony2 Collection Form, you must specify the type of the field and notify the form that it is the collection of the particular field.

This is done when you want to customize the Collection Form to the particular data that was submitted by the user.

images (5)

There is a requirement that on selecting a particular category form (add products) button, the list of all subcategories defined in the category should be displayed. So that the user can select one or multiple subcategory(s) form list of a particular category. Doctrine2 helps generate model classes from already existing databases.

One category can be mapped with many subcategories. For the form registration, the user can select as many categories and its corresponding subcategories.

For using Doctrine2 ORM, you’ll need to add the Doctrine2 metadata like using the One-To-Many relationship annotation, many subcategories can be mapped into one category. The user selects his choice of category and hence, it’s subcategories while filling up the form.

Yeah! EAV Model can be quite confusing, but it’s not that hard. You’ll get it right!





First of all, we created three entities namely TrademarkQuestionnaire, Category, Subcategory. Here “TrademarkQuestionnaire”  -> “Entity”, “Category” ->  “Attribute”  and “SubCategory” -> “Value”.

We create a Category.php and SubCategory.php. Category and SubCategory has one-to-many relationship. Then we combine both of these Category and SubCategory in another entity class called CategorySubCategory.php. In this CategorySubCategory class Category and CategorySubCategory is mapped by one-to-many bidirectional relationship and CategorySubCategory and SubCategory class has one-to-many bidirectional relationship. Now in our main Entity class Trademarkquesionnaire.php we map Trademarkquesionnaire class with combined class entity CategorySubCategory class with many-to-many bidirectional relationship. Thus, we are able to embed EAV system using Symfony2 Collection Form.



Steps for creating the category entity and mapping of different fields of this entity with others:

Create a category entity as follows: Category.php

    1. We declared field such as name in this class.
    2. Further we did the mapping of this entity with another entity SubCategory in one-to -many relationship.
    3. Again, we did the mapping of this entity with another entity named as CategorySubCategories in one-to-many relationship.
    4. This entity is further mapped with another entity Trademarkquesionnaire in many-to-one relationship.
    5. We define two function _toString() and _construct() for converting object into string and to initialize the components respectively.



We then generated the getter and setter method for the above fields by running the given commands on the Symfony2 console:

Create the subcategory entity as follows: Subcategory.php.

  1. We declared the field ‘productName’ which is of type string.
  2. Then we map the SubCategory entity with the previously created entity category in many-to-one relationship.
  3. SubCategory entity is further mapped with CategorySubCategories entity in many-to-many relationship.
  4. We define  function _toString() function to convert objects into string.
  5. Now generate the get() and set() methods for the above defined fields by compiling the following commands

Create the subcategory entity as follows: CategorySubCategory.php

First, we map this entity with category entity in many-to-one relationship. Again, we map this entity with subcategories entity in many-to-many relationship. This particular entity is mapped again with Trademarkquesionnaire entity in many-to-many relationship. Now,define the two function _toString() function to convert objects into string .



Create the subcategory entity as follows: trademarkQuestionnaire.php

We map the Trademarkquestionnaire entity with another entity CategorySubategory in many-to-many relationship. We define two function _toString() function to convert objects into string.

After defining the functions we generated the get() and set() methods for the above defined field by running the following commands on console.

Symfony2 Collection Form is to manage a group of similar items. In this file various actions were created through CRUD generation among which we modified the CreateAction() and UpdateAction() methods.

Both the methods, before final submissions of data, the Subcategory entity’s fields were called to enter the values of subcategory for each category. As we have done one to many mapping of category entity with subcategory entity.




  1. CategoryType.php
    We import the following statement on the top of file to inherit SubcategoryType form in this file
    In build form function we add collection of subcategory and allow the add and delete functionality to be true, which is by default false.
  1. SubcategoryType.php
  1. CategorySubCategoryType.php
    It is the most important part of the blog, because it is the actual form by which we are able to combine the Category(attribute) and SubCategory(value) in  Trademarkquesionnaire(entity) form where we select a category(attribute), the relevant subcategories(values) are displayed.
    First we import the following statement on top of the file to use the fields of subcategory entity. In bulidform function we add two EventListners for PRE_SET_DATA and PRE_SUBMIT . We define two functions onPreSubmit() and onPreSetData() to get the values to be stored for different categories and subcategories. Also, we define a function Addelements() to various subcategories to single category.
    This function also arrange various subcategories into ascending order.
Then after we will have to manage to make a system such that after selecting a category, the relevant subcategories are displayed. This can be achieved by writing the Ajax function and passing the Ajax url to the controller method.

We have need to create a controller method to fetch the subcategories by category in AJAX call .

  1. TrademarkQuestionnaireType.php
    In these form type, we import the following statement on the top of file. In bulidform() function, we add Collection Form of categorySubCategory so that the User can select many Subcategory for a single Category.






After creating the above mentioned entities we write the controllers of these entities.



  1. CategoryController.php

In this Controller we have to import the following statement on the top of the file :



  1. Category

//index.html.twig (No modifications were made to the default code)


We called a class of type subcategory.
Created a Subcategory collectionHolder to store various Subcategories of a particular category into this Collection Form.
Create a link for adding many Subcategories for a particular category. This is achived through addTagForm() method.
Create a link for deleting Subcategories for a particular category. This is achived through addTagForm Delete link() method.

Css Block

Javascript Block

Show.html.twig(No modifications were made to the default code.)

Edit.html.Twig (New file is added in the new.html.twig file)

  1. CategorySubCategory

Index.html.twig(No modifications were mode to the default code.)

In this file, inside the javascript tags, document ready() function is called for enabling the Subcategories to be added to the Category for every loading of the page.


Css block


Show.html.twig (No modifications were made to the default code)
Edit.html.Twig (No modifications were made to the default code)


  1. TrademarkQuestionnaire

In new.html.twig file, we add a new button loaded as add + to select categories and its respective Subcategories. For this purpose inside the javascript tags. We created a collectionHolder variable and added to it, CategorySubCategoryAddValueLink. Also, we added a delete link to the collectionHolder to delete any existing SubCategory and Categories. For both these functionalities, we defined two separate functions categorySubCategories AddValueForm and CategorySubCategory AddTagFormDeleteLink().


Well, I hope you’ve embeded your EAV model in Symfony2 Collection Form. Hope this helped!

Please do share the tutorial if you appreciate it at all. And for any doubts, please mention your query in the comments section below.

Happy Reading!




  1. How To Override FOSUserBundle – TUTORIAL
  2. How To Create A Multi Column Design Layout Using Wookmark jQuery – TUTORIAL
    wookmark jquery
  3. How To Create A Business Application In Data Grid – ORO Platform – TUTORIAL
    Oro Platform
  4. Image Flip Using jQuery

Comments 2

  1. Pingback: Creating Business Application In Data Grid - ORO Platform

  2. Pingback: How To Upgrade Bootstrap 2 To Twitter Bootstrap3 Build

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.