Implementing UUID in Joomla Print

  • 3

For large projects you may decide that a UUID (or GUID) is a more practical solution to table record identification than the standard AUTO_INCREMENT integer value. While MySQL provides some support for UUID via the UUID() function, the actual implementation is rather hairy and requires some massaging to work with the Joomla system. Since the Joomla database classes are all written to assume the integer for primary key columns, we are forced to use one of two methods. For both examples we will use the following SQL table.

{wbshowcode:Joomla/UUID_In_Joomla/table.sql}

  • Implement a JTable class extension (FASTER)
    By creating a JTable class extension we are given the opportunity to add UUID handling to the store operation for all tables within our component. This is our personal choice for large projects, but it does replace the standard Joomla JTable store() function. This means that future Joomla updates to the store function will need to be manually evaluated and incorporated if necessary. In our example we have stripped the tree storage features from the store function, as most applications will not need those handlers.  You will notice in the example we are identifying the use of the primary key `uuid` and then creating a UUID value prior to insertion.

    {wbshowcode:Joomla/UUID_In_Joomla/JTableExtensionClass.php}

    Using the above JTable extension you would then implement your component tables as you normally would.

    {wbshowcode:Joomla/UUID_In_Joomla/tableUsingExtensionClass.php}


  • Implement MySQL Database Triggers (CLEANER)
    Since there is no "AUTO_INCREMENT" option for the `uuid` primary key column in our table we need to create one. The purpose of the database triggers will be to generate the new UUID upon record insertion, and to cache the newly created UUID for later retrieval from a "last_insert_id" style operation. The first trigger generates a UUID and assigns the new UUID() to the `uuid` field prior to performing the insert. Using this method we would implement the table within Joomla the same as any other table, assigning the `uuid` field as the primary key and allowing Joomla to perform the standard insertion.

    {wbshowcode:Joomla/UUID_In_Joomla/triggers.sql}

    With the triggers in place we will need to implement our Joomla table class with a slight modification to collect the last insert id before returning from the store() operation.

    {wbshowcode:Joomla/UUID_In_Joomla/tableUsingTriggers.php}


Speed Differences

While both are very efficient approaches to the problem, there is a noticable 10% gain in speed by using the JTable extension option.   On our test server we received the following result from a Joomla insertion loop where we established a new class reference and performed a store 1000 times.

  • Standard Joomla ID (Integer Auto Increment) = 7.9 Seconds
  • Using JTable Extension Class (UUID) = 8.22 Seconds
  • Using the MySQL Triggers (UUID) = 8.85 Seconds






Was this answer helpful?

« Back