The above example introduces the basic method of writing code templates using Codesmith, which implements a more practical code template that automatically generates the required ActiveRecord classes for the Yii Framework through the database.
In this example, we modify the Yii Framework Development tutorial (26) Database-active record sample, the original example is the manual writing employee.php ActiveRecord.
First add a C # project for the project (any type, we just use this project to contain the Codesmith project), and then add a Codesmith project and a codesmith template. Then refer to the Codesmith Tutorial (1): Overview Using schema Explorer to add a data connection, this example connects to the Chinook database:
The code template created PHPACTIVERECORD.CST defines a property tablename (database table name) and replicates the Yii Framework development tutorial (26) Database-active The definition of employee.php in the record example uses attributes, as follows:
<%@
Template language= "C #" targetlanguage= "PHP" debug= "False"%> <%@ property
name= "tablename" type= "System.String" description= "Table name"%>
<?php class <%= tablename%> extends Cactiverecord
{public
static function model ($className =__class__)
{return
Parent::model ($className);
}
Public Function tablename ()
{return
' <%= tablename%> '}
}
? >
<script runat= "template" > Public
Override String GetFileName () {return
tablename + ". php"; c19/>}
</script>
You can then create the corresponding ActiveRecord PHP class for any data table by defining the properties of the TableName. But it's still a manual one to configure the table name. This example automatically generates the corresponding ActiveRecord for all tables through a primary template and a template from the connection database
Use the specific usage of master and subordinate templates later, simply said that the child template is equivalent to a child function, the main template is similar to the main function can call the child functions, the main template by calling the child template, to the child template properties so that multiple files can be generated.
To create a code template YIIDATAMODEL.CST as the primary template, you need to register in the main template to use the child template first:
<%@ Register name= "ActiveRecord"
template= "PHPACTIVERECORD.CST" mergeproperties= "false" %>
The complete code is as follows:
<%@ codetemplate language= "C #" targetlanguage= "Text" description= "List all database Tables"%> IMP ORT namespace= "System.IO"%> <%@ Property name= "SourceDatabase" type= "Schemaexplorer.databaseschema" Categor Y= "Context" description= "Database containing" the tables. "%> <%@ Register name=" ActiveRecord "template=" phpact IVERECORD.CST "mergeproperties=" false "%> <%@ Assembly name=" Schemaexplorer "%> <%@ Import Namesp
Ace= "Schemaexplorer"%> <script runat= "template" > public string Firstlettertoupper (String str) { if (str!= null) {if (str). Length > 1) return char. ToUpper (str[0]) + str.
Substring (1); else return str.
ToUpper ();
return str; } </script> <% for (int i = 0; i < SourceDatabase.Tables.Count i++) {%> <% string Name= Firstlettertoupper (sourcedatabase.Tables[i]. Name); %> <% string filename= @ ". /activerecorddemo/protected/models/"+name+". php; %>//Instantiate the sub-template <% ActiveRecord = this. Create<activerecord> ();%> <% activerecord.tablename= name; %> <% Activerecord.rendertofile (filename,true); %> <%}%>