Getting started with ArangoDB and Symfony 2 – part 3

API, PHP Tags: , , , Leave a comment
This is part 3 (of 4) of an introduction to the use of ArangoDB together with Symfony 2. You’ll find the links to the other parts of this tutorial at the end of this text.
You can download the completed demo at Github.

Working with forms

The next example shows the use of ArangoDB with Symfony’s form component.

This is how our form will look like. The topics are represented as a text field and the user is required to separate them by commas. This delivers questionable user experience, but leads to an interesting question: how can we transform our topics array from our movie document to a text field? And: we’ll need it at least two times, both for creating and editing movies.

The “naive” approach might look like this:

We create a standard Symfony 2 form class in the controller

And use this form class

Note line 21 where we convert the string with the topics into an array and assign this array to our ArangoDocument.

This works, but is quite verbose, it will not work with different movie types having slightly different attributes and we have to use very similar code again when we implement the “edit movie” feature.

In the next sections of the tutorial, we’ll remove all the set(“xxx”) methods from the controller and replace it by a populate method in a new entity class. And we’ll implement a better way to convert the topics array to a string (for the ui representation) and vice versa.

Converting the topics list with a Symfony data transformer

First for the topics2array problem: Symfony’s form component comes with data transformer. Data transformers are used to convert form data between app data, norm data and client data (the Symfony manual has more general information on data transformers).

Our TopicsToTopiclistTransformer converts the topics string to an array and vice versa.

Once we have created the transformer we can assign it to the topics list element in the form class (line 12).

Now Symfony will automatically run the transformer. Problem 1 is solved. :-)

The end

Congrats, you have reached the end of part 3 the tutorial. There is more:

Tweet about this on TwitterShare on Google+Share on RedditShare on LinkedInBuffer this page
Dorthe Lübbert

About Dorthe Lübbert

web developer from Cologne, interested in alternative databases & member of the ArangoDB team - mainly helping out with website and community related stuff.