PHP ORM框架ezpdo(2)之EZPDOSQL
其實這個框架的所謂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()
SUM()
例子
$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函數
.
總結
以上是生活随笔為你收集整理的PHP ORM框架ezpdo(2)之EZPDOSQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个简单的Ajax开发框架
- 下一篇: 未来中国最受宠的人才