[Authored by Alex_g]
[01/04/15 – Editor’s note: Beezwax no longer maintains FMCakeMix, but the open source project became maintained by Atsushi Matsuo. You may find more information, and his contact info on the FMCakeMix GitHub project.]
Developing FileMaker applications for the web can be a sometimes slow and arduous process. The available custom web publishing tools from FileMaker and open source offerings such as FX.php are very helpful in bringing basic data and interactivity to a FileMaker driven page or form, but their structure leads to poor code maintainability and repetitive code writing practices for larger and highly customized web projects. In recent years web application frameworks have become the tool of choice for rapidly developing robust browser-based applications. These tools provide the structure for good code keeping practices and already include code for common tasks such as the basic CRUD (create, read, update, and delete) operations required to work with a database. The common architecture followed by most frameworks separates the data-source and data access from presentational code by way of an application logic component. This type of architecture is known as MVC (Model-View-Controller) and the frameworks that support it will often also support a large number of common database sources, including MySQL, Oracle, PostgreSQL, etc. Unfortunately FileMaker is never among these offerings and is not natively supported in an MVC framework.
In an attempt to bring the current generation of web development tools to FileMaker web integration I decided to create a FileMaker database driver to work with the CakePHP framework, thus allowing for the rapid, maintainable, and scalable coding practices an MVC framework affords. A similar solution, RFM, produced by Geoff Coffey, exists for the popular Ruby on Rails framework, a framework that is very similar to Cake, but uses ruby instead of the highly prevalent PHP programming language. I would also suggest looking at RFM and the Rails framework if considering an MVC approach to FileMaker web development.
The Cake FileMaker driver is based on Chris Hansen’s FX.php and as such makes calls to FileMaker through xml and then returns data in a manner that Cake can understand. This means an application using the driver could mix data-sources that relate data between FileMaker and other types of databases or allow an application to be easily converted between data-sources, such as a conversion from FileMaker to a more scalable database, such as mysql, as the application grows. In addition, there are a myriad of useful features and tools that the CakePHP frameworks supports. See the CakePHP website for more information.
Without going into a full-blown tutorial, I’m going to walk through the configuration and setup of a basic Cake based application capable of talking to FileMaker to demonstrate how quickly we can establish a connection to FileMaker and utilize the frameworks features.
The first step will be to configure our connection to the FileMaker server and associate a database connection with the driver file. This is done with the following code in the database.php configuration file. In this instance I’m keeping the driver in the dbo folder of the Cake core and a copy of fx.php in my applications vendors folder.
var $filemaker = array( 'driver' => 'filemaker_dbo', 'persistent' => false, 'dataSourceType' => 'FMPro7', 'scheme' => 'http', 'port' => 80, 'host' => '127.0.0.1', 'login' => 'web_access', 'password' => 'webPass', 'database' => 'FMServer_Sample', 'prefix' => '', );
The model component of the MVC structure is a representation of our data and defines a table within our data source. The example below represents a table of books from the FMServer_Sample.fp7 database that installs with FileMaker Server. The model has been defined in standard cake fashion with a few additional modifications. We need to tell the model which FileMaker table we’re representing and the name of the layout where we can access this table. We then have optionally defined has-many relationships to other models and added validation rules for our field data.
class Book extends AppModel { var $name = 'Book'; var $useDbConfig = 'filemaker'; var $primaryKey = 'ID'; // FileMaker specific configuration var $fm_Table = 'ENGLISH_NATURE'; var $defualtLayout = 'web_books_general'; // relationships var $hasMany = array( 'Comment' => array( 'foreignKey' => '_fk_book_id' ), 'History' => array( 'foreignKey' => '_fk_book_id' ) ); // validation var $validate = array( 'Title' => array( 'rule' => 'notEmpty' ), 'Author' => array( 'rule' => 'notEmpty' ) ); }
This is now enough code to allow the controller to Create, Read, Update, and Delete books from the FileMaker database. To make a presentation of these operations we’ll need to add some additional code to the controller and view components. The books_controller, which handles the application logic for the books model, will be the location where we write code to interact with the book records from our FileMaker database. Below we’re defining the index function, or the standard function called when we visit /books at our application domain. We tell the method to return a paginated list of the books within our database. Additionally we could also define controller functions for viewing, adding, editing, deleting, or any operation on books we see fit.
function index() { $this->Book->recursive = 0; $this->set('books', $this->paginate('Book')); }
To present the list of books and to add the pagination user controls for advancing pages and sorting results by column we’ll use the cake paginator helper in our view file.
counter(array( 'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true) )); ?> >
sort(‘Title’);?> | sort(‘Author’);?> | sort(‘Publisher’);?> |
---|---|---|
The resulting rendered view of our paginated and sortable list of books:
From here the task of providing the additional controller methods for adding, editing or removing books is almost trivial and can be followed directly from the CakePHP blog tutorial.