Tomas S. Tomas S. - 1 year ago 93
PHP Question

Symfony2 and Selectize.js: Clearest way to persist new items in entity field type?

In Symfony2 I have

BandType
, where I add the entity
Tag
:

->add('tags', 'entity', [
'label' => 'Tags',
'class' => 'DbBundle:Tag',
'property' => 'title',
'multiple' => true,
])


This generate multiple select element, where I can choose existing tags from database (Doctrine). But I need to add new tags dynamicaly, which don't exist yet.

On a client side I use jQuery plugin Selectize.js, which allows me to add new tag to select box. But after submit form the new tags are not saved.

So my question is - what is the clearest way to persist new items from select box (entity field type)?

Answer Source

Use a Data Transformer for your entity. And in the reverseTransform method, if you don't find the newly added band, simply create it there instead of throwing a TransformationFailedException.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download