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()
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函数
.

自主封装的PHP ORM框架,面向对象的PDO数据库操作,API框架,支持Get/Post/Put/Delete多种请求方式。 代码示例: <?php use Models\User; require '../application.php'; require '../loader-api.php'; //适合查询,如:获取用户列表或者单个用户信息 execute_request(HttpRequestMethod::Get, function() { $action = request_action(); //判断是否存在 if ($action == 1) { list($type, $value) = filter_request(array( request_int('type', 1, 2, 3), //1.用户名 2.邮箱 3.手机号 request_string('value'))); $type_field_map = array( 1 => User::$field_username, 2 => User::$field_email, 3 => User::$field_phone ); if ($type == 2 && !is_email($value) || $type == 3 && !is_mobilephone($value)) { die_error(USER_ERROR, $type_field_map[$type]['name'] . '格式无效'); } $user = new User(); $user->set_where_and($type_field_map[$type], SqlOperator::Equals, $value); $result = $user->exists(create_pdo()); echo_result($result ? 1 : 0); //存在返回1,不存在返回0 } //查询单条信息 if ($action == 2) { list($userid) = filter_request(array( request_userid())); //查询单条数据 $user = new User($userid); //set_query_fields可以指定查询字段,下面两种写法均可 //$user->set_query_fields('userid, username, email'); //$user->set_query_fields(array(User::$field_userid, User::$field_username, User::$field_email)); //还可设置where条件进行查询 //$user->set_where_and(User::$field_status, SqlOperator::Equals, 3); //$user->set_where_and(User::$field_truename, SqlOperator::IsNullOrEmpty); //$user->set_where_and(User::$field_age, SqlOperator::In, array(27, 29)); //$user->set_where_and(User::$field_regtime, SqlOperator::LessThan, '-6 month'); //创建数据库连接 $db = create_pdo(); $result = $user->load($db, $user); //也可以用Model类的静态方法 //$result = Model::load_model($db, $user, $user); if (!$result[0]) die_error(PDO_ERROR_CODE, '获取用户信息时数据库错误'); if (!$user) di
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值