Marc,
really appreciated what you've done! It saves no end of time. I have Ext Designer but found it extremely slow so I tend to hand code a lot of my projects. I've expanded on what you've provided to add the store.
The my app structure is as follows:
\MYAPP\app\view
\view\ui
\store
\php\ <-- all get and set methods go in here
I use a lot of views in SQL and I always convert datetime to varchar: CONVERT(varchar, myDate, 120) as dtMyDate
I have therefore added a filter to look for 'dt' at the start of the field name and set that as a date type.
Code:
SET ANSI_NULLS ONGO
SET QUOTED_IDENTIFIER ON
GO
/*======================================================================
ExtJS Model Generator
This SP accepts a database object (table, view) name's parameter and
generates an ExtJS model based on the object's fields
Sample Usage:
USE Northwind
EXEC procGenerateExtJsModel 'Shippers'
Author: Marc Fearby
Based on procGenerateEntityClass by Leon C. Tayson
http://www.planet-source-code.com/vb...=1039&lngWId=5
======================================================================*/
CREATE PROCEDURE [dbo].[procGenerateExtJsModel]
@ObjectName varchar(100)
AS DECLARE @name varchar(35), @type varchar(35)
DECLARE objCursor CURSOR
FOR
SELECT sc.name, st.name type FROM syscolumns sc INNER JOIN systypes st ON st.xusertype = sc.xusertype WHERE Id=OBJECT_ID(@ObjectName)
DECLARE @declarationCodes varchar(8000)
SET @declarationCodes = ''
OPEN objCursor
FETCH NEXT FROM objCursor INTO @name, @type
DECLARE @extType varchar(35) -- ExtJS type
DECLARE @ctorParms varchar(4000)
SET @ctorParms = ''
IF @@FETCH_STATUS <> 0
BEGIN
CLOSE objCursor
DEALLOCATE objCursor
PRINT 'Error... Please check passed parameter'
RETURN
END
WHILE @@FETCH_STATUS = 0
BEGIN
SET @extType = CASE
WHEN @type LIKE '%char%' OR @type LIKE '%text%' THEN 'string'
WHEN @type IN ('decimal', 'numeric') THEN 'float'
WHEN @type LIKE '%money%' THEN 'float'
WHEN @type = 'bit' THEN 'boolean'
WHEN @type LIKE '%int%' THEN 'int'
WHEN @type LIKE '%uniqueidentifier%' THEN 'string'
WHEN @type LIKE '%datetime%' THEN 'date'
ELSE 'auto' END
-- Remove the 'tb' part of the table name
IF LEFT(@ObjectName, 2) = 'tb' OR LEFT(@ObjectName, 2) = 'vw' BEGIN
SET @ObjectName = SUBSTRING(@ObjectName, 3, LEN(@ObjectName))
END
IF LEFT(@name, 2) = 'dt' SET @extType = 'date'
SET @declarationCodes = @declarationCodes + ' { name: ''' + @name + ''', type: ''' + @extType + ''' },' + CHAR(13)
IF @extType = 'date' BEGIN
SET @declarationCodes = LEFT(@declarationCodes, LEN(@declarationCodes) - 4)
SET @declarationCodes = @declarationCodes + ', dateFormat: ''Y-m-d H:i:s''},' + CHAR(13)
END
FETCH NEXT FROM objCursor
INTO @name, @type
END
PRINT 'Ext.define(''mdl' + @ObjectName + ''', {'
PRINT ' extend: ''Ext.data.Model'','
PRINT ' fields: ['
PRINT LEFT(@declarationCodes, LEN(@declarationCodes) - 2)
PRINT ' ]'
PRINT '});'
PRINT ''
PRINT ''
PRINT 'Ext.define(''MYAPP.store.jsStore' + @ObjectName + ''', {'
PRINT ' extend: ''Ext.data.Store'','
PRINT ''
PRINT ' constructor: function(cfg) {'
PRINT ' var me = this;'
PRINT ' cfg = cfg || {};'
PRINT ' me.callParent([Ext.apply({'
PRINT ' model: ''mdl' + @ObjectName + ''','
PRINT ' storeId: ''jsStore' + @ObjectName + ''','
PRINT ' proxy: {'
PRINT ' type: ''ajax'','
PRINT ' url: ''/MYAPP/php/get' + @ObjectName + '.php'','
PRINT ' reader: {'
PRINT ' type: ''json'','
PRINT ' root: ''rows'','
PRINT ' totalProperty: ''total'''
PRINT ' }'
PRINT ''
PRINT ' }'
PRINT ''
PRINT ' }, cfg)]);'
PRINT ' }'
PRINT '});'
PRINT ''
PRINT ''
PRINT '// Save As: jsStore' + @ObjectName + '.js'
CLOSE objCursor
DEALLOCATE objCursor