Thursday, 22 May 2008

CakePHP and Access sitting in a tree...

Introduction

A mentioned in a previous post, I'm currently working on a CakePHP powered website. One of the requirement for the project was the ability to create reports using a 3rd party reporting product.

We looked at several options but finally settled on Microsoft Access because a) the client already had it, and b) they had some experience of using Access to generate reports so the learning curve was less steep.

Getting the Data into Access


The CakePHP application uses a MySQL database so the natural step was to use ODBC (Open Database Connectivity) to link the tables into an Access MDB (Microsoft Database). From there, the client could use the Access GUI (Graphical User Interface) to create custom queries and reports.

I won't cover installing the MySQL ODBC Driver (otherwise know as MySQL Connector/ODBC) and linking the tables as there's plenty of documentation on the web that covers this step, and the instructions are slightly different for each version of Access.

Linking tables via ODBC is trivial in Access, but ODBC doesn't provide any schema information to Access for it to build the relationships between the (in this case 30+) tables. As the application is still in development the schema, although solid, may be subject to change. Adding the relationships between 30+ tables by hand using the relationship editor is both very time consuming and more importantly, very susceptible to human error.

Couple that with the need to keep updating the relationship diagram in access to reflect any changes in the MySQL database and you soon see this is going to be a nightmare to do by hand. Sounds like a job for automation!

It's been quite a while since I'd done any VBA (Visual Basic for Applications), the last being back in the days of Access2000 but it didn't take too long to get up to speed.

Automate it

My CakePHP application follows the CakePHP database naming conventions which might seem a little odd to someone who is used to Access, but I find them very intuitive.
  • Tables have plural names (posts, programmes, cities, people)
  • Primary keys are named 'id'
  • Foreign keys are named the singular table name followed by _id (post_id, programme_id, city_id, person_id)
The only potentially tricky bit is inflecting the singular foreign key names to their plural table names. A simple "plural" function takes care of that. In this case, none of my tables had 'non-standard' plural names (all plurals were the singular with 's' appended: post_id -> posts, project_id->projects, etc.).

With this information, it was relatively simple to create a VBA function that runs through all the fields in the linked tables and builds relationship objects that relate the foreign keys to their respective tables:

The Code
Function buildRelations()

Dim rel As Relation ' the relationship we are building
Dim tbl As TableDef ' the table we are processing
Dim fld As Field ' the field we are processing
Dim strForeignTable As String ' name of the foreign table
Dim intRelId As Integer ' counter used to create unique relationship names

' remove any exisiting relationships

Do While CurrentDb.Relations.Count > 0

CurrentDb.Relations.Delete (CurrentDb.Relations(0).Name)

Loop


' rebuild relationships
intRelId = 0

For Each tbl In CurrentDb.TableDefs

For Each fld In tbl.Fields

If Right(fld.Name, 3) = "_id" Then

' found foreign key

' generate foreign table name (plural)
strForeignTable = pluralise(Left(fld.Name, Len(fld.Name) - 3))

' build the relationship object
intRelId = intRelId + 1

Set rel = CurrentDb.CreateRelation("rel" & intRelId, tbl.Name, strForeignTable, dbRelationDontEnforce)
rel.Fields.Append rel.CreateField(fld.Name)
rel.Fields(fld.Name).ForeignName = "id"

' append the relationship to the database relationship collection
CurrentDb.Relations.Append rel

End If

Next

Next

MsgBox "Relationships have been rebuilt"

End Function

Private Function pluralise(str As String) As String

pluralise = str & "s"

End Function
You may need to expand the pluralise() function to deal with odd pluralisations (city_id->cities, person_id->people) in your own schema.

One word of caution. If you use Access' Query builder to build queries with linked tables, Access 'helpfully' tried to guess the primary and foreign keys for you which will mess things up royally. You can switch off this feature by unchecking 'Enable AutoJoin' in the Access Options dialog.

OpenCms – Initial Notes from OpenCms Days Conference: May 5th 2008, Cologne

OpenCms, the open source content management system, forms that basis for many of our successful web projects.

I attended (and presented at) the first international OpenCms conference in May 2008.

Here are some notes that I took at the event.

A complete conference programme can be found on the OpenCms Days website.The slides and video recordings of the presentations will be made available on the OpenCms website in the near future.

The key theme for the conference was that OCMS is a powerful platform that is widely used on major global, mission critical sites (e.g. pokemon.com and gardena.de) but the developer community is rather weak in comparison to that associated with other open source products (i.e. Drupal) with OCMS developers tending to ‘hang onto’ their code base, for the following reasons:

  • concerns about IPR - some companies have built successful businesses around sophisticated bespoke code and they are concerned that releasing that code will compromise their success
  • failure to coordinate between OpenCMS developers - with the result that those organisations that want to provide code to the community are not sure what is valuable and whether it is worthwhile making the effort to release
  • no clear sense among the community of whether there is a wider market for modules that OpenCMS developers have authored - for example Futurate have an ‘events module’, but is the market large enough to justify making it available for free or at a charge....?
There was wide agreement among delegates that the situation must change if the product is to move forward, and development of the OpenCms website to more actively support community code exchange seems the most likely outcome.

Delegates were reminded that the current OpenCms website contains a small list of modules and extensions, as does the OpenCms wiki. A comprehensive single point of call for developers and business managers does not exist however, nor does any method of determining the quality of any released code through approaches such as peer review or rating.

For readers who are interested in learning more about freely available code/documentation for OpenCms, the following links may be of interest:

I felt that the following modules/extensions are worth singling out as of particular interest:

Other notes

  • Alkacon are planning to release a number of further modules/extensions before the end of June, including a calendar, a basic blog, drag and drop of content elements, and a survey module. An indication of the direction for OpenCms over the next couple of years can be found in Alex Kandzior’s presentation when it is made available for download.
  • Packt Publishing have released a new OpenCms book for OpenCms 7 developers - and a review will be appearing in this blog over the next few weeks.
  • Version 7.0.5 of OepnCms will be available in July 08. Version 7.5 early 09.

I’ll make an announcement here when the slides and video from OpenCms days have been made available.

Thursday, 1 May 2008

Free RSS Module for OpenCMS

To coincide with my presentation at OpenCMS Days 2008, we have released our first completely free, open source module for OpenCMS 7.

This module which forms a core component of our recent project for Film London allows OpenCMS developers to easily create RSS feeds from OpenCMS content and external databases.

We have provided compiled code, source code and complete documentation.

Thanks to Steve Osguthorpe for preparing the code for release in time for the event in Cologne.