[HOW TO] MySQL Trigger
8 posts by 2 authors in: Forums > CMS Builder
Last Post: August 18, 2010 (RSS)
By richb - August 16, 2010
Re: [richb] [HOW TO] MySQL Trigger
By Chris - August 16, 2010 - edited: August 16, 2010
The simplest solution would be to use the existing "num" field. You could add some silly-huge number to this to make your SKUs all look uniform.
I think that using a trigger might not work because of how CMS builder updates records right after inserting them. I'm not entirely sure if this would work or not, but the good news is that it's super easy to do with a tiny custom plugin (see attached.)
I hope this helps! Please let me know if you have any questions.
Chris
Re: [chris] [HOW TO] MySQL Trigger
By richb - August 16, 2010
Re: [richb] [HOW TO] MySQL Trigger
By Chris - August 17, 2010
The plugin I attached will generate "unique" sku's (see the documentation for PHP's uniqid function) for new records on any table that has a field called 'sku'. If your field is actually called 'SKU' instead of 'sku', you will need to change 'sku' to 'SKU' in the plugin.
If you have other tables with 'sku' fields which you don't want auto-generated, let me know and I'll customize the plugin to only work for your Products table.
To install the plugin, save it in your /cmsAdmin/plugins/ directory, log into CMS Builder, go to Admin -> Plugins, find it there (it's called "Custom Generate SKU") and activate it.
Please let me know if you have any questions.
Chris
Re: [chris] [HOW TO] MySQL Trigger
By richb - August 17, 2010
Table: Products
Field: prodname [textfield]
Field: sku [textfield] (plugin to update this field on new rec)
Field: qty [textfield]
Thank you
richb.
Re: [richb] [HOW TO] MySQL Trigger
By Chris - August 17, 2010 - edited: August 17, 2010
I've modified the plugin to only function on a table called 'Products' and have attached it.
Chris
Re: [chris] [HOW TO] MySQL Trigger
By richb - August 17, 2010
Thanks again this was perfect.
Re: [richb] [HOW TO] MySQL Trigger
By Chris - August 18, 2010
Glad that helped! :)
There isn't documentation on creating plugins quite yet. Our plugin system is still relatively new and we're still settling on how we want to do things. Many plugins will require advanced knowledge of CMS Builder internals (or the determination to examine the source until such knowledge is gained.)
The first parameter is known as the "hookName". If you want to see where it's called, search the source code for 'record_presave'. You'll see that it's called by doAction(). There's also a sister function to that called applyFilters(). You can find out what other options there are for hookNames by searching the CMS Builder source code for doAction( and applyFilters(.
Please note: this is not for the faint of heart! ;)
Chris