We do a lot of work integrating FileMaker into web applications. All of our new projects use the Symfony framework for this, and as a consequence the Doctrine DBAL / ORM. We’ve written Doctrine drivers for both the PHP API and the Data API which makes development much quicker (and swapping clients from one to the other a single change in a config file FTW!)
Often times we’re only responsible for the web side of a project and some of our clients aren’t so good at data normalisation so we end up needing to generate some rather large entities – today the entity that needed more than 50 ‘fields’ (don’t ask – legacy FileMaker, legacy API) finally tipped Director Steve Winter over the edge so we wrote a couple of scripts to streamline the process!
Export a FileMaker layout to JSON
Right now this is a two-step process (but we’ll be adding this to our Symfony FileMaker Bundle really soon). Begin by adding this simple JsonThisLayout
script to your API interface file (you’re using data separation for web interface files right?). You can copy and paste it from the demo file.
Set Variable [ $layout ; Value: Get(LayoutName) ]
Set Variable [ $$fieldData ; Value: JSONSetElement ( "{}" ;
["layout" ; $layout ; JSONString ] ;
["fields" ; "[]" ; JSONArray]
)]
Set Variable [ $fields ; Value: FieldNames ( "" ; $layout) ]
Set Variable [ $fieldIndex ; Value: 0 ]
Loop
Exit Loop If [ Let ( $fieldIndex = $fieldIndex + 1 ; $fieldIndex > ValueCount ( $fields ) ) ]
Set Variable [ $$fieldData ; Value:
Let ( [
field = GetValue ( $fields ; $fieldIndex ) ;
info = Substitute ( FieldType ( "" ; field ) ; " " ; ¶ ) ;
info = GetValue ( info ; 2 ) ;
fieldJson = JSONSetElement ( "{}" ;
[ "field" ; field ; JSONString ];
[ "type" ; info ; JSONString ]
)
] ;
JSONSetElement ( $$fieldData ; "fields[" & $fieldIndex -1 & "]" ; fieldJson ; JSONObject )
)
]
End Loop
Now that you have a JSON representation of your layout, save that to a .json file somewhere on your local machine.
Convert that to an Entity
Next head over to github and grab our FM Layout to Entity converter. To get things up and running
mkdir layout-to-entity && cd layout-to-entity
git clone git@github.com:matatirosolutions/fm-layout-to-entity.git .
composer install
You can then run the convert command, passing in the the required parameters
bin/console convert [options] [--] <file> <destination> <entity>
file
– the location of the .json file you created abovedestination
– where the generated entity should be savedentity
– the name of the entity to generate
You can also pass an optional -r=true
to also generate a corresponding repository. If you do set this to true, then the script expects to find Entity
and Repository
folders in destination
into which it will save the corresponding files. For example you might use
bin/console convert ./order.json ../MySymfonyProject/src Order -r=true
What next?
We see this as the first step in this tool and will probably add it to our Symfony FileMaker Bundle so that the whole process can be run without needing to interact with FileMaker directly. We’ll probably also extend things so that it doesn’t do a complete overwrite of the file (you get a warning about that currently) which would allow for the entity to be updated with only new fields added to a layout.
Hopefully this will be helpful to others – feedback, issues, pull requests all welcome.