课程表

CodeSmith课程

工具箱
速查手册

CodeSmith Yii简单模板

当前位置:免费教程 » 软件/图像 » CodeSmith

自动生成Yii Framework ActiveRecord类简单模板

上例介绍了使用 CodeSmith 编写代码模板的基本方法,本例实现一个较为实用的代码模板,通过数据库自动为 Yii Framework 生成所需要的 ActiveRecord 类。

本例通过修改 Yii Framework 开发教程(26) 数据库-Active Record 示例,原例是手工编写 Employee.php ActiveRecord。

首先为工程添加一个 C# 项目(任意类型,我们只是利用这个项目来包含 CodeSmith 项目),然后添加一个 CodeSmith 项目和一个 CodeSmith 模板。然后参考 CodeSmith 概述 使用Schema Explorer 添加一个数据连接,本例连接到 Chinook 数据库:

第13张

创建的代码模板 PhpActiveRecord.cst 定义个属性 TableName(数据库表名),复制 Yii Framework 开发教程(26) 数据库-Active Record 示例中 Employee.php 的定义并使用属性,代码如下:

  1. <%@ Template Language="C#" TargetLanguage="PHP" Debug="False" %>
  2. <%@ Property Name="TableName" Type="System.String" Description="Table name" %>
  3. <?php
  4. class <%= TableName %> extends CActiveRecord
  5. {
  6. public static function model($className=__CLASS__)
  7. {
  8. return parent::model($className);
  9. }
  10. public function tableName()
  11. {
  12. return '<%= TableName %>';
  13. }
  14. }
  15. ?>
  16. <script runat="template">
  17. public override string GetFileName() {
  18. return TableName + ".php" ;
  19. }
  20. </script>

这时就可以通过定义 TableName 的属性给任意数据表生成对应的 ActiveRecord PHP 类了。 不过这还是要手工来一个一个来配置表名。 本例通过一个主模板和一个从模板的方式通过连接数据库自动为所有的表生成对应的 ActiveRecord

使用主从模板的具体用法后面再介绍,简单的说子模板相当于子函数,主模板类似于主函数可以调用子函数,主模板通过调用子模板,传给子模板属性从而可以生成多个文件。

创建一个代码模板 YiiDataModel.cst 作为主模板,使用子模板首先需要在主模板中进行注册才能使用:

  1. <%@ Register Name="ActiveRecord" Template="PhpActiveRecord.cst" MergeProperties="false" %>

完整代码如下:

  1. <%@ CodeTemplate Language="C#" TargetLanguage="Text"
  2. Description="List all database tables" %>
  3. <%@ Import Namespace="System.IO" %>
  4. <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema"
  5. Category="Context" Description="Database containing the tables." %>
  6. <%@ Register Name="ActiveRecord" Template="PhpActiveRecord.cst"
  7. MergeProperties="false" %>
  8. <%@ Assembly Name="SchemaExplorer" %>
  9. <%@ Import Namespace="SchemaExplorer" %>
  10. <script runat="template">
  11. public string FirstLetterToUpper(string str)
  12. {
  13. if (str != null)
  14. {
  15. if(str.Length > 1)
  16. return char.ToUpper(str[0]) + str.Substring(1);
  17. else
  18. return str.ToUpper();
  19. }
  20. return str;
  21. }
  22. </script>
  23. <% for (int i = 0; i < SourceDatabase.Tables.Count; i++) { %>
  24. <% string name= FirstLetterToUpper(SourceDatabase.Tables[i].Name); %>
  25. <% string filename= @"../ActiveRecordDemo/protected/models/"+name+".php"; %>
  26. // instantiate the sub-template
  27. <% ActiveRecord activeRecord = this.Create<ActiveRecord>();%>
  28. <% activeRecord.TableName= name; %>
  29. <% activeRecord.RenderToFile(filename,true); %>
  30. <% } %>

FirstLetterToUpper 为C#函数,主要是把数据库表名的第一个字母变为大写(纯 C# 代码)。

SchemaExplorer 为 CodeSmith 提供的数据库访问库,可以用来获取数据库 Schema 的信息,如包含的表名,字段属性,主键外键等(后面具体介绍)

在主模板中,通过 ActiveRecord 来访问子模板(名字 ActiveRecord 为注册子模板时定义) ,使用 this.create 创建子模板实例,然后传入 TableName 属性,调用 RenderToFile 将子模板的结果写道指定的文件中。

此时在 CodeSmith.csp 中添加主模板,配置数据库为 Chinook,然后生成代码

  1. Rendering output 'YiiDataModel'...
  2. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Album.php
  3. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Artist.php
  4. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Customer.php
  5. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Employee.php
  6. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Genre.php
  7. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Invoice.php
  8. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Invoiceline.php
  9. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Mediatype.php
  10. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Playlist.php
  11. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Playlisttrack.php
  12. Generated: D:\tmp\ActiveRecordDemo\ActiveRecordDemo\protected\models\Track.php
  13. Generated: D:\tmp\ActiveRecordDemo\CodeSmith\YiiDataModel.txt
  14. Done rendering outputs: 1 succeeded, 0 failed, 0 skipped (1

刷新项目可以看到自动生成的代码文件

第14张

本例只是为每个数据表生成最简单的 ActiveRecord,如果需要生成关联 ActiveRecord,可以进一步根据表之间的关系为每个 ActiveRecord 生成所需的 relations 方法,后面有时间进一步介绍。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号