jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

其实这个框架的所谓ezpdosql就是hibernate的HSQL咯,没啥的,所以照罗列一次,没啥特别的

首先是from子句
$m = epManager::instance();
 

$books = $m->find("from Book as b where b.title = ?", $title);
 
//like的例子
$books = $m->find("from Book as b where b.title like 'Intro%'");
 
// null的例子
$books = $m->find("from Book as b where b.title is null");
 

$books = $m->find("from Book as b where b.pages < ?", $pages);
 
$books = $m->find("from Book as b where b.title like ? and b.pages < ?", $title, $pages);

之后是支持in参数了

$books = $m->find("from Book as b where b.price in (2.50, 100.01)");

$books = $m->find("from Book as b where b.author.name in ('Joe Smith', 'Jane Smith')");

in里面也支持数组
books = $m->find("from Book as b where b.price in (?)", array(2.50, 100.01));

$books = $m->find("from Book as b where b.author.name in (?)", array('Joe Smith', 'Jane Smith'));


当然要支持sort和limit了
// find books and sort by book id (default ascending order)
$books = $m->find("from Book as b where b.title like ? order by b.id", $title);
 
// find books and sort by id in ascending order
$books = $m->find("from Book as b where b.title like ? order by b.id asc", $title);
 
// find books and sort by id in desscending order
$books = $m->find("from Book as b where b.title like ? order by b.id desc", $title);
 
// find books and sort by id in desscending order and limit to the first two only
$books = $m->find("from Book as b where b.title like ? order by b.id desc limit 0, 2", $title);

支持以下的聚合函数
AVG(),
COUNT(),
MAX(),
MIN()
S
UM()
例子
$cost = $m->find("sum(price) from Book where title like '%PHP%'");
$num_pages = $m->find("sum(pages) from Book where title like '%PHP%'");
$num_books = $m->find("count(*) from Book where title like '%PHP%'");
$cost_per_page = $cost/$num_pages;
$cost_per_book = $cost/$num_books;

更复杂一点的例子,这里涉及到关联对象的HQL

$authors = $m->find("from Author as a where a.contact.zipcode = '12345');

这里,假设Author类和Contact类有一对一的关系,zipcode是contact类的一个属性,这里是找出所有作者的联系方式中邮政编码为12345的记录了。

在已经有双向关联的对象中,如何用ezpdoz的SQL呢,举例子如下
假如要找所有smith作者写的书,则
$books = $m->find("from Book as b where b.authors.contains(a) and a.name = 'Smith'");
因为authors和books是多对多关系,这里要用contains函数
.

posted on 2007-03-09 11:30  jackyrong的世界  阅读(758)  评论(0编辑  收藏  举报