关注PHP的ORM框架 -- propel

最近在看PHP的 rails式的框架 symfony,这是个整合的框架,ORM框架用的是 propel.基于xml配置的PHP ORM.
粗略的看了一下还是比较勥的.
官方文档

支持: 至少php 5.2.x
Lisense : lgpl v3

安装:
推荐使用pear 安装. 需要 phing 支持

//添加更新源
pear channel-discover pear.phpdb.org
pear channel-discover pear.phing.info

//安装propel生成器,类似hibernate
pear 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 = 项目名称

# The Propel driver to use for generating SQL, etc.

propel.database = mysql

# This must be a PDO DSN
propel.database.url = mysql:dbname=test
propel.database.user = root
# propel.database.password = 


迁移时可以使用
propel-gen ./ sql
来迁移数据库结构
当然也可以使用
propel-gen ./ datadump
将数据导出来
详细说明请看 这里

---------------------------------我是分割线----------------------------


配置:
类似hibernate,propel也需要一个映射的配置文件 schema.xml
查看它的 DTD

根节点
<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"/>
<!-- 配置实体对应的关系 这里是one to one-->
  <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>
 <!-- 如果安装了 pear Log 可以注销这部分. 用来保存pear的错误
 <log>
  <type>file</type>
  <name>/path/to/prople.log</name>
  <ident>propel-bookstore</ident>
  <level>7</level>
 </log>
 -->
 <propel>

<!-- 配置数据源 -->
  <datasources default="bookstore">
   <datasource id="bookstore"> <!-- this ID must match <database name=""> in schema.xml -->
    <adapter>sqlite</adapter> <!-- sqlite, mysql, myssql, oracle, or pgsql -->
    <connection>
     <dsn>sqlite2:/path/to/bookstore.db</dsn> <!-- the PDO connection DSN for database -->
    </connection>
   </datasource>
  </datasources>
 </propel>
</config>


更多配置说明

---------------------------------我是分割线----------------------------


使用:
终于到使用这个方便的框架的时候了,所有操作都和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 引用
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();

你可能感兴趣的:(Hibernate,mysql,框架,PHP,orm)