PHP ORM框架ezpdo(2)之EZPDOSQL
其實(shí)這個(gè)框架的所謂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參數(shù)了
$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里面也支持?jǐn)?shù)組
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'));
當(dāng)然要支持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);
支持以下的聚合函數(shù)
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;
更復(fù)雜一點(diǎn)的例子,這里涉及到關(guān)聯(lián)對(duì)象的HQL
$authors = $m->find("from Author as a where a.contact.zipcode = '12345');
這里,假設(shè)Author類和Contact類有一對(duì)一的關(guān)系,zipcode是contact類的一個(gè)屬性,這里是找出所有作者的聯(lián)系方式中郵政編碼為12345的記錄了。
在已經(jīng)有雙向關(guān)聯(lián)的對(duì)象中,如何用ezpdoz的SQL呢,舉例子如下
假如要找所有smith作者寫的書,則
$books = $m->find("from Book as b where b.authors.contains(a) and a.name = 'Smith'");
因?yàn)閍uthors和books是多對(duì)多關(guān)系,這里要用contains函數(shù)
.
總結(jié)
以上是生活随笔為你收集整理的PHP ORM框架ezpdo(2)之EZPDOSQL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一个简单的Ajax开发框架
- 下一篇: 未来中国最受宠的人才