最近在看PHP的 rails式的框架 symfony,这是个整合的框架,ORM框架用的是 [url=http://propel.phpdb.org/trac/wiki]prope[/url]l.基于xml配置的PHP ORM.
粗略的看了一下还是比较勥的.
[url=http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3]官方文档[/url]
支持: 至少php 5.2.x
Lisense : lgpl v3
[color=olive][size=xx-large]安装:[/size][/color]
推荐使用pear 安装. 需要 phing 支持
//添加更新源pear channel-discover pear.phpdb.orgpear channel-discover pear.phing.info//安装propel生成器,类似hibernatepear install --alldeps phpdb/propel_generator//安装运行支持库pear install --alldeps phpdb/propel_runtime
从现有的数据库中迁移:
propel支持从现有的数据库结构中生成 schema.xml .可以使用 自带的引擎
propel-gen ./ reverse
或者 cropel
propel-gen ./ creole
需要一个配置文件 build.properties
propel.project = 项目名称
迁移时可以使用
propel-gen ./ sql
来迁移数据库结构
当然也可以使用
propel-gen ./ datadump
将数据导出来
详细说明请看 [url=http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/HowTos/ExistingDatabases]这里[/url]
---------------------------------我是分割线----------------------------
[color=olive][size=xx-large]配置:[/size][/color]
类似hibernate,propel也需要一个映射的配置文件 schema.xml
查看它的[url=http://propel.phpdb.org/trac/browser/branches/1.3/generator/resources/dtd/database.dtd]DTD[/url]
根节点
<database name="bookstore" defaultIdMethod="native"></database>
看一个简单的例子
schema.xml
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><database name="bookstore" defaultIdMethod="native"> <table name="book" description="Book Table"> <column name="book_id" type="integer" primaryKey="true" autoIncrement="true" required="true" description="Book Id"/> <column name="title" type="varchar" size="255" required="true" description="Book Title"/> <column name="isbn" type="varchar" size="24" required="true" phpName="ISBN" description="ISBN Number"/> <column name="publisher_id" type="integer" required="true" description="Foreign Key for Publisher"/> <column name="author_id" type="integer" required="true" description="Foreign Key for Author"/> <foreign-key foreignTable="publisher"> <reference local="publisher_id" foreign="publisher_id"/> </foreign-key> <foreign-key foreignTable="author"> <reference local="author_id" foreign="author_id"/> </foreign-key> </table> <table name="publisher" description="Publisher Table"> <column name="publisher_id" type="integer" required="true" primaryKey="true" autoIncrement="true" description="Publisher Id"/> <column name="name" type="varchar" size="128" required="true" description="Publisher Name"/> </table> <table name="author" description="Author Table"> <column name="author_id" type="integer" required="true" primaryKey="true" autoIncrement="true" description="Author Id"/> <column name="first_name" type="varchar" size="128" required="true" description="First Name"/> <column name="last_name" type="varchar" size="128" required="true" description="Last Name"/> </table></database>
运行propel的时候需要另一个配置文件 runtime-conf.xml
<?xml version="1.0" encoding="ISO-8859-1"?><config> <propel> <datasources default="bookstore"> <datasource id="bookstore"> <adapter>sqlite</adapter> <connection> <dsn>sqlite2:/path/to/bookstore.db</dsn> </connection> </datasource> </datasources> </propel></config>
[url=http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/RuntimeConf]更多配置说明[/url]
---------------------------------我是分割线----------------------------
[color=olive][size=xx-large]使用:[/size][/color]
终于到使用这个方便的框架的时候了,所有操作都和hibernate非常相似
首先在使用的脚本前加载库,可以放在你的应用的启动器里头
// 设置domain类所在的文件夹 这里假设在build/classes里面.set_include_path("/path/to/bookstore/build/classes" . PATH_SEPARATOR . get_include_path());require_once 'propel/Propel.php';Propel::init("/path/to/bookstore/build/conf/bookstore-conf.php");
一些常用的操作: C.R.U.D [url=http://propel.phpdb.org/trac/wiki/Users/Documentation/1.3/BasicCRUD]引用[/url]
include_once 'build/class/Author.php';//新建$author = new Author();$author->setFirstName("Jack");$author->setLastName("London");$author->save();//获取, AuthorPeer: AuthorPeer 相当域pojo的域类,包含了curd的静态方法, 是自动生成的$author = AuthorPeer::retrieveByPK(1);//查询$c = new Criteria();$c->add(AuthorPeer::FIRST_NAME, "Karl");$c->add(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);$authors = AuthorPeer::doSelect($c); //或 查询$c = new Criteria();$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Leo");$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME, array("Tolstoy", "Dostoevsky", "Bakhtin"), Criteria::IN);// combine them$cton1->addOr($cton2);// add to Criteria$c->add($cton1);//直接使用SQL$con = Propel::getConnection(DATABASE_NAME);$sql = "SELECT books.* FROM books WHERE NOT EXISTS (SELECT id FROM review WHERE book_id = book.id)"; $stmt = $con->prepare($sql);$stmt->execute();$books = BookPeer::populateObjects($stmt);//删除$author = AuthorPeer::retrieveByPK(1);AuthorPeer::doDelete($author);//或者$author->delete();