今天上午继续讲PEAR DB,然后是和SMARTY的结合,中午停电,闪人。

    把昨天和今天的代码放上来,结合了PEARDB和SMARTY的,文件放在根目录下的peardataobject目录,根目录下需要有smarty的libs文件夹

    peardataobject目录下有4个文件夹,models和schema是PEARDB用的,template和template_c是SMARTY用的

    所有文件除了list.html在template下,其他都在peardataobject下。

需要注意的有

1,list,php下

while($employee->fetch())
{
 $array[]=clone $employee;
}

这句里面必须加clone,建立新的内存空间

2,$employee->update(DB_DATAOBJECT_WHEREADD_ONLY);

或者$employee->delete(DB_DATAOBJECT_WHEREADD_ONLY);

括号里必须加上DB_DATAOBJECT_WHEREADD_ONLY,否则操作不了,这句的意思是只对当前选定的数据进行操作

3,需要先运行orm,php对数据库中的表建类

   文件清单:

    config.php

<?php
/**
 * 与pearDatoObject结合的数据库配置文件
 *
 */
define(ROOT,$_SERVER['DOCUMENT_ROOT']);
//设定查找路径,如果不设定此调用pear相关模块时会出现找不到文件的情况
set_include_path(
    get_include_path() .
    PATH_SEPARATOR . ROOT.'/libs/PEAR/' .
    PATH_SEPARATOR . ROOT.'/libs/PEAR/DB/' .
    PATH_SEPARATOR . ROOT.'/libs/PEAR/DB/DataObject/' .
    PATH_SEPARATOR
    );
//echo get_include_path();
require_once(ROOT."/libs/PEAR/PEAR.php");//pear的基类库
require_once(ROOT."/libs/PEAR/DB.php");//pear的数据操作类基类库
require_once(ROOT."/libs/PEAR/db/DataObject.php");//pear的面向对象库
require_once(ROOT."/libs/PEAR/DB/DataObject/Generator.php");//完成表->对象自动映射的代码

/*ORM
object  relation mapping*/
//定义用到的参数
$dbMachine="localhost";
$dbUser="root";
$dbPassword="123456";
$database="employee";
$classLocation=ROOT."/peardataobject/models";//存放生成的表对象的文件夹
$schemaLocation=ROOT."/peardataobject/schema";//存入方案文件所在的文件夹

$_DB_DATAOBJECT['CONFIG']['database'] = "mysql://$dbUser:$dbPassword@$dbMachine/$database";
$_DB_DATAOBJECT['CONFIG']['schema_location'] = $schemaLocation;
$_DB_DATAOBJECT['CONFIG']['class_location'] = $classLocation;
$_DB_DATAOBJECT['CONFIG']['class_prefix'] = "T";//要不要取决于习惯

//获取静态成员变量,并以引用的形式赋与$opetion 
$options = &PEAR::getStaticProperty('DB_DataObject','options');
//让前面的配置起作用
$options=$_DB_DATAOBJECT['CONFIG'];


//定义SMARTY
require_once(ROOT.'/libs/SMARTY/Smarty.class.php');
$smarty=new Smarty();
$smarty->template_dir=ROOT.'/peardataobject/template';
$smarty->compile_dir=ROOT.'/peardataobject/template_c';
$smarty->left_delimiter='<!--{';
$smarty->right_delimiter='}-->';
 

 

orm.php

<?php
require_once("config.php");
//实例化ORM映射
$generator = new DB_DataObject_Generator();
//开始生成表对象文件
$generator->start();
$generator->free();
 

 

insert.php

<?php
require_once('config.php');
require_once('./models/Employeereg.php');
$employee=new TEmployeereg();
$employee->query("set names utf8");
$employee->uuid=uniqid(true);
$employee->name='笨笨02';
$employee->gender='2';
$employee->insert();
$employee->free();
?>

 

list.php

<?php
require_once('config.php');
require_once('./models/Employeereg.php');
$employee=new TEmployeereg();
$employee->query("set names utf8");

$employee->find();
$array=array();
while($employee->fetch())
{
 $array[]=clone $employee;
}
$employee->free();
 
$smarty->assign('employee',$array);
$smarty->display('list.html');
?>

 

list.html

<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta name="GENERATOR" content="Zend Studio" />
<!--<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>title</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FF9966" vlink="#FF9966" alink="#FFCC99">
<!--{section name=section1 loop=$employee}-->
<!--{$employee[section1]->name}--><br />
<!--{/section}-->
</body>
</html>

 

update.php

<?php
require_once('config.php');
require_once('./models/Employeereg.php');
$employee=new TEmployeereg();
$employee->query("set names utf8");
$employee->whereAdd("uuid='14889dd1c3d093'");
$employee->name='帅哥01';

$employee->update(DB_DATAOBJECT_WHEREADD_ONLY);
$employee->free();

 

delete.php

<?php
require_once('config.php');
require_once('./models/Employeereg.php');
$employee=new TEmployeereg();
$employee->whereAdd("uuid='14889dd1c3d093'");
$employee->delete(DB_DATAOBJECT_WHEREADD_ONLY);
$employee->free();