Sniff, Sniff ... What's In Your Database?


One of the more under-utilized features of Zen Cart is the sniffer class, instantiated in both the admin- and store-side as $sniffer during the auto_load start-up. This class allows you to interrogate the database to determine if

  1. A table already exists, via function table_exists.
  2. A field in a table already exists, via function field_exists.
  3. A field in a table already exists and is of a specific type, via function field_type.

If you're developing a plugin that makes database-related changes, this class can help you determine, for instance, whether the plugin's new field updated_by is already present in the orders_status_history (TABLE_ORDERS_STATUS_HISTORY) table:

if (!$sniffer->field_exists(TABLE_ORDERS_STATUS_HISTORY, 'updated_by') {
    {insert the updated_by field}

Using Zen Cart's built-in class also insulates your plugin from any database API changes in the future!

Copyright © 2012-2024 Vinos de Frutas Tropicales. Powered by Zen Cart