此脚本基于example配置文件中的值创建一个到数据库的连接,显示如下. example.ini [DB_DataObject] database = mysql://root:@localhost/example schema_location = /dataobjects/schema/ class_location = /dataobjects/ require_prefix = /dataobjects/ class_prefix = DataObjects_ extends_location = DB/DataObject.php extends = DB_DataObject 自动构建数据库模式
包含两个过程,构建数据库的对象-关系映射,和从example数据库user表中自动创建一个类.在表中的所有字段 名将成为类成员变量. 创建适当的模式:
C:\PHP\PEAR\DB\DataObject>C:\PHP\php.exe createTables.php \ C:\dataobjects\example.ini 这将生成User.php文件:
<?php /** * Table Definition for user * www.knowsky.com */ require_once 'DB/DataObject.php'; class DataObjects_User extends DB_DataObject { ###START_AUTOCODE /* the code below is auto generated do not remove the above tag */ var $__table = 'user'; // table name var $user_Id; // int(11) not_null primary_key auto_increment var $first_Name; // string(30) not_null var $last_Name; // string(40) not_null var $email; // string(100) not_null /* Static get */ function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('DataObjects_User',$k,$v); } /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE } ?> 它还会为user表模式生成example.ini配置文件: [user] user_Id = 129 first_Name = 130 last_Name = 130 email = 130 [user__keys] user_Id = N Smarty files It's time to create several files for Smarty:
Smarty文件 现在是创建几个Smarty文件的时候了: include.php 1 <? 2 require('Smarty.class.php'); 3 $smarty = new Smarty; 4 $smarty->template_dir = 'templates/'; 5 $smarty->compile_dir = 'templates_c/'; 6 $smarty->config_dir = 'configs/'; 7 $smarty->cache_dir = 'cache/'; ?> 此脚本实例化了一个新Smarty对象.设置Smarty属性. index.php 1 <? 2 require("include.php"); 3 $smarty->assign('TITLE','ACCESS MySQL DATABASE IN THREE TIERS WITH PHP'); 4 $smarty->assign('HEADER','WHAT WISH DO ?'); 5 $smarty->display('index.tpl'); ?> |