Link | autoctype |
Author | Richard Moore |
Category | Custom Tag |
Version | 8.5.x |
License | Public Domain |
Posted | 09 Aug 2008 |
Updated | 09 Aug 2008 |
More by this author... |
This is a custom type that makes it easy to update,add, and delete records in a given table. It simply reads a table that is passed in as a parameter and creates a custom type for that table that allows save, clone, delete, and a couple of other tags. Each of the tables should use 'id' as the keyfield. (But this could be easily changed). Also it does NO validation. This can be done via a subclass. I use these methods on virtually every custom type. I also like to store most of my information as instance variables to help with debugging. There are a few things to know: NOTE:THE DATABASE IS A GLOBAL VARIABLE 'tsdb'. This could be passed in, etc. per your code. ->save This is used to create AND update. It does NOT create a complex keyfield, but uses the straightforward incremental id. ->delete This just deletes the record ->clone This creates a copy of the record ->outputProperties This puts out all the properties as a nice table :-) Credits: Jason & Bill
Make the table: DROP TABLE IF EXISTS `test_table`; CREATE TABLE `test_table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `description` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; var:'tsdb' = 'mydatabase'; // The database connected to You can create an object.. var('myobject') = (autoctype(-table='test_table')); The add some data... $myobject->description = 'Deco is home'; Then save to the database... $myobject->save; The see the id... output:$myobject->id; e.g. 25 Now create a new object... var('mysecondobject') = (autoctype(-table='test_table',-id=25)); Now update the description.. $mysecondobject->description = 'Deco is out'; now, update the database... $mysecondobject->save;
Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
define_type('autoctype',-optional='id',-required='table'); local( 'id' = integer, 'code' = string, 'result' = boolean, 'column_names' = array, 'inline_parameters' = array, 'table' = string, 'error' = string); define_tag('onCreate'); self->table = #table; local('ivars') = @self->properties->first; inline( -database = $tsdb, -sql = 'select * from 'self->table' limit 1'); loop(field_name(-count)); select(field_name(loop_count,-type)); case('text'); #ivars->insert((field_name(loop_count)) = string); case:('number'); #ivars->insert((field_name(loop_count)) = decimal); case:('date/time'); #ivars->insert((field_name(loop_count)) = (field_name(loop_count,-type))); case; #ivars->insert((field_name(loop_count)) = string); /select; /loop; /inline; if(local_defined('id')); self->id = #id; else; self->id = 0; /if; self->code = 'select * from
')); #outputString->(append($k->first)); #outputString->(append(' | ')); #outputString->(append($k->second)); #outputString->(append(' |
No comments