JA's K2 Search and Filter add-on is a nice - and powerful tool. But has an annoying problem (at least in version 1.0.7): When one edit a K2 item which have extra fields, on save an ugly error 1062 database error is popping up. The component is working correctly, the modified date is saved, but the user  doing the update gets seriously confused.

After doing some research, the bug was found, there are 2 unconditional inserts done by the jak2filter plugin: the plugin is not checking, if the data is already inserted in his taxonomy tables, and tries to save it on each change made for existing items. since some of the fields are set to be unique, the MySQL server responds with the above ugly error - naturally.

The fix would be easy, by using the "INSERT IGNORE" MySQL command - and here is the problem! The core Joomla team sticks to broad database support, and the core JDatabaseQuery class does not support the INSERT IGNORE syntax, to keep the database compatibility.

The JAK2Filter plugin's code is a weird mix of the API compliant database queries using the core JDatabaseQuery class and raw SQL commands, so wouldn't be a big damage to it to rewrite the JDatabaseQuery specific code to raw SQL commands and solve the problem this way (anyway, who cares database compatibility, when you need to FIX one given site in one known environment???), but since Darwin we know, that sex and laziness are the two main driving forces in the evolution.

So, since no sex in this - anyway, not in the classic sense - what can a lazy old Joomla guru do then? Is simpler, than you can imagine. Just locate the buggy code (looking like this)

$query->columns(array($db->quoteName('type'), $db->quoteName('title'), $db->quoteName('asset_id'), $db->quoteName('option_id'), $db->quoteName('num_items')));
$query->values($db->quote($type) . ', ' . $db->quote($title). ', ' . $db->quote($asset_id). ', ' . $db->quote($option_id). ', ' . $db->quote(0));

And replace the last line with this one:

$db->setQuery(str_replace('INSERT INTO', 'INSERT IGNORE INTO', $query));

What you say? It's simple enough?

Category: K2 hacks