[autoctype]

Description

Link: [autoctype]
Author: deco rior
Category: Custom Tag
Version: 8.5.x
License: Public Domain
Posted: Aug. 09, 2008
Updated: Jan. 01, 0001
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

Parameters

-table string, required
-id integer, optional

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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 <table> where id = <id>';
		self->code->(replace:'<id>',self->id);
		self->code->(replace:'<table>',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' = '<table style="font-size:10px;margin:0px;">';
            var:'z' = self->properties->get:1;
            iterate($z,(var:'k'));
                #outputString->(append('<tr>'));
                #outputString->(append('<td style="padding: 0px 10px 1px 10px;">'));
                #outputString->(append($k->first));
                #outputString->(append('</td><td style="padding: 0px 10px 1px 10px;">'));
                #outputString->(append($k->second));
                #outputString->(append('</td><tr>'));
                #outputString->(append('<tr>'));
            /iterate;
            #outputString->(append('</table>'));
            return(@#outputString);
        /define_tag;
/define_type;

 

Comments

none

Email:


Password:



Newest

Most Popular