Lasso Soft Inc. > Home

[autoctype]

Linkautoctype
AuthorDeco Rior
CategoryCustom Tag
Version8.5.x
LicensePublic Domain
Posted09 Aug 2008
Updated09 Aug 2008
More by this author...

Description

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

Sample Usage

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;

Source Code

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  where id = ';
		self->code->(replace:'',self->id);
		self->code->(replace:'
',self->table); inline: -database = $tsdb, -sql = self->code; self->column_names = field_names; loop(field_name(-count)); var:'x' = (field_name(loop_count)); self->$x = field($x); /loop; if:found_count == 1; self->result = true; else; self->result = false; /if; /inline; /define_tag; define_tag('save'); iterate(self->column_names,(var('nam'))); if($nam != 'id'); self->inline_parameters->(insert($nam = self->$nam)); /if; /iterate; if:self->id > 0; inline( -database=$tsdb, -table=self->table, -keyfield='id', -keyvalue=self->id, self->inline_parameters, -update); self->code=action_statement; self->error = error_currenterror; /inline; else; inline( -database=$tsdb, -table=self->table, -keyfield='id', -keyvalue=self->id, self->inline_parameters, -add); self->id = keyfield_value; self->code=action_statement; self->error = error_currenterror; /inline; /if; /define_tag; define_tag('clone'); iterate(self->column_names,(var('nam'))); if($nam != 'id'); self->inline_parameters->(insert($nam = self->$nam)); /if; /iterate; if:self->id > 0; self->error = 'No item active'; else; inline( -database=$tsdb, -table=self->table, -keyfield='id', -keyvalue=self->id, self->inline_parameters, -add); self->id = keyfield_value; self->code=action_statement; self->error = error_currenterror; /inline; /if; /define_tag; define_tag('delete'); inline( -database=$tsdb, -table=self->table, -delete, -keyfield='id', -keyvalue=self->id); self->id = 0; self->code=action_statement; self->error = error_currenterror; /inline; /define_tag; define_tag('outputProperties'); // This outputs a table into the page that shows all the variables local:'outputString' = '
'; var:'z' = self->properties->get:1; iterate($z,(var:'k')); #outputString->(append('')); #outputString->(append('')); #outputString->(append('')); /iterate; #outputString->(append('
')); #outputString->(append($k->first)); #outputString->(append('')); #outputString->(append($k->second)); #outputString->(append('
')); return(@#outputString); /define_tag; /define_type;

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft