How I integrated myFMApiLibrary for PHP with Symfony 3.4

Lesterius recently published the myFMApiLibrary for PHP. The myFMApiLibrary for PHP is a REST client library for PHP that allows you to easily interact with the FileMaker Data API and integrate it faster. But how do you use it?

Lucie Guilbert
Apr 18 · 9 min read

In this article I will show you how I use it with Symfony 3.4, a popular Web Application framework based on a set of PHP Components.

Feel free to improve it.

Prerequisites

This article assumes that you already have experience with Symfony.

Before using the DataApi from FileMaker, make sure that your FileMaker Server has a proper SSL certificate installed.

First you need to get the library using composer:

You will also need JMS serializer library (follow documentation found here) and doctrine annotations to be able to link FileMaker fields with your PHP properties:

Then create 4 parameters in parameters.yml file: database_api_url, database_name, database_user and database_password.

Create services

Now that everything is fine, we’ll create 2 services in services.yml:

  • One to set the myFMAPI library
  • The other one to set-up the Event Subscriber.

The first one needed is used to set-up the myFMAPI library:

The second one is used to set-up the connection to the DataApi to be able to use our first service:

Here follows the class RequestEventSubscriber.php that must be put in src/AppBundle/EventSubscriber repository:

Create entities

With the old FileMaker Php library I used to create a table with the field names and the property names. In Symfony, I can use annotations to link fields with properties.

Usually you need a lot of entities. I think it is best to make an abstract class for all of them. In this class, I create methods to manage the internal FileMaker id and to get the field names found in FileMaker.

Here follows my AbstractEntity.php in src/AppBundle/Entity:

Now in my entity, I’ll need to use:

Every class that extends the AbstractEntity Class will need an “IdNameClass” property. The name of the property must start with “id”, followed by the name of the class in camel case, otherwise it will not work. As an example if I have Class Attendee, I’ll need a property with the name $idAttendee.

Next, I’ll put my FileMaker field names in “Serializer\SerializedName” annotations.

In “Serializer\Type” we store the data type. Here‘s the types you can find:

  • int
  • string
  • Datetime (with UTC format)
  • boolean

In “Serializer\Groups” you define the actions that can be done on the field:

  • create
  • update
  • internal
  • excluded
  • default

These groups are managed in the AbstractRepository class. The “default” group is set if you didn’t define it.

The group will be used when needed, in a “create” case to insert some data you choose not to update, or in an “internal” case where you only need to retrieve data (i.e. modification timestamps).

Creating Repositories

You need to create a repository for your entity. Like entities, I made a AbstractRepository to simulate a database call, similar to what I did for Doctrine.

For my own reading comfort I prefer writing all my database functions in my repositories files.

Here follows my AbstractRepository.php in src/AppBundle/Repository:

How to use it?

I created 2 functions in my BaseController.php:

  • one to call the myFMApiLibrary-for-PHP service and connect to the database.
  • one to close the connection (don’t forget this part!).

Then in my controller, I can create my repository object and my object:

What about error messages?

The error messages you could have should be explicit (and with their FileMaker error code). DataApi works the same way as another FileMaker Client. As an example, two users can’t edit the same record at the same time, and will throw an error.

Conclusion

Follow these steps, and you will be able to easily connect your Symfony to the FileMaker 17 DataAPI (with myFMApiLibrary for PHP), and significantly save your work time, which is good for you, but even better for your clients!

What were your thoughts on this tutorial? Please let me know in the comments.

Don’t forget to subscribe to follow our reviews and updates of this article once the FileMaker 18 DataAPI will be released.

Some Tips

If you choose “int” as “Serializer\Type” annotation, be sure the field is always filled. “Empty” is not an int nor a boolean, change to “string” to avoid this.

@Serializer\Type(“DateTime<’m/d/Y’, ‘Europe/Paris’>”) can be @Serializer\Type(“DateTime<’m/d/Y H:i:s’, ‘Europe/Paris’>”), for example.

If you don’t put @Serializer annotation, the property won’t be linked to FileMaker.

And please, don’t forget to set stricter privileges to your DataApi user for more security.

Lesterius

We are creative business consultants who co-create FileMaker Platform based solutions with our customers.

Lucie Guilbert

Written by

They say I'm effervescent so I disappear in several bubbles - Web/FileMaker Consultant at Lesterius

Lesterius

Lesterius

We are creative business consultants who co-create FileMaker Platform based solutions with our customers.