所谓的谓词Predicate

//  ------ 所谓的谓词 ------
// 条件 case.3
$where = new \Zend\Db\Sql\Where();
$expression = new \Zend\Db\Sql\Predicate\Expression("field1=? and field2=?",array('a','b'));
$where->addPredicates($expression,\Zend\Db\Sql\Predicate\PredicateSet::OP_OR); // 条件 case.4
$predicate = new \Zend\Db\Sql\Predicate\Predicate();
$predicate = new \Zend\Db\Sql\Predicate\Expression("field1=? and field2=?",array('a','b'));
$where->addPredicates($predicate,\Zend\Db\Sql\Predicate\PredicateSet::OP_OR);

所谓的添加、删除、修改、查询

/**
* 所谓的添加、删除、修改、查询
* @author zhouzhian
*
*/
class CrudTest{ /**
* 下面这个代码能执行的条件是:
* 有注入“服务管理器”,即:可正常调用$this->getServiceLocator()
*/
public function testTableGateway(){
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new \Album\Model\Album());
$tableGateway = new \Zend\Db\TableGateway\TableGateway('album', $dbAdapter, null, $resultSetPrototype);
} /**
* 条件
* 查询条件、删除条件、修改条件、查询插入条件
*/
public function testWhere(){
// ---------条件---------
$id = 0; // 条件 case.0
$where = array('id' => $id); // 条件 case.1
$where = new \Zend\Db\Sql\Where();
$where->addPredicates(array('id' => $id), \Zend\Db\Sql\Predicate\PredicateSet::OP_AND); // 条件 case.2
$where = new \Zend\Db\Sql\Where();
$where->addPredicates(array(
'field1=? and field2=?' => array('a','b'),// field1=a and field2=b
'field2' => null, // field2 IS NULL
'field3' => array(1,2,3), // field3 in(1,2,3)
'field4' => '4' // field4=4
), \Zend\Db\Sql\Predicate\PredicateSet::OP_OR); // 条件 case.3
$where = new \Zend\Db\Sql\Where();
$expression = new \Zend\Db\Sql\Predicate\Expression("field1=? and field2=?",array('a','b'));
$where->addPredicates($expression,\Zend\Db\Sql\Predicate\PredicateSet::OP_OR); // 条件 case.4
$predicate = new \Zend\Db\Sql\Predicate\Predicate();
$predicate = new \Zend\Db\Sql\Predicate\Expression("field1=? and field2=?",array('a','b'));
$where->addPredicates($predicate,\Zend\Db\Sql\Predicate\PredicateSet::OP_OR); // 条件 case.5
// WHERE "field0" IS NULL OR "field3" IN (:where1, :where2, :where3) OR (field3=:where4 and field4=:where5)
$where1 = new \Zend\Db\Sql\Where();
$expression1 = new \Zend\Db\Sql\Predicate\Expression("field3=? and field4=?",array('a','b'));
$where1->addPredicates($expression1,\Zend\Db\Sql\Predicate\PredicateSet::OP_AND);
$condition = array(
'field0' => null,
'field3' => array(1,2,3),
$where1,
);
$where = new \Zend\Db\Sql\Where();
$where->addPredicates($condition,\Zend\Db\Sql\Predicate\PredicateSet::OP_OR);
} /**
* 查询
*/
public function testSelect(){
$where = $this->testWhere(); // ---------查询---------
// 执行 case.0,这种方式不能自己指定要获取的列
// SELECT "album".* FROM "album" WHERE field1=:where1 and field2=:where2
$resultSet = $this->tableGateway->select($where); // 执行 case.1,指定要查询的列
// SELECT "album"."field1" AS "field1", "album"."field2" AS "field2_alias" FROM "album" WHERE field1=:where1 and field2=:where2
$select = $this->tableGateway->getSql()->select();
$select->columns(array('field1','field2_alias'=>'field2'));
$select->where($where); // where ...
$resultSet = $this->tableGateway->selectWith($select); // $rowset === Zend\Db\ResultSet\ResultSet // 执行 case.2,指定别名
// SELECT "table1_alias"."field1" AS "field1", "table1_alias"."field2" AS "field2_alias" FROM "album" AS "table1_alias" WHERE field1=:where1 and field2=:where2
$select = new \Zend\Db\Sql\Select();
$select->from(array('table1_alias'=>'album'));
$select->columns(array('field1','field2_alias'=>'field2'));
$select->where($where); // where ...
$resultSet = $this->tableGateway->selectWith($select); // 执行 case.3,分组、排序、分页限制
// SELECT "album"."field1" AS "field1", "album"."field2" AS "field2_alias"
//   FROM "album" WHERE field1=:where1 and field2=:where2
//   GROUP BY "field1", "field2"
//   ORDER BY "field1" ASC, "field2" DESC
//   LIMIT :limit OFFSET :offset
$select = $this->tableGateway->getSql()->select();
$select->columns(array('field1','field2_alias'=>'field2'));
$select->where($where);
$select->group(array('field1','field2')); // group by ....
$select->order(array('field1'=>'ASC','field2'=>'DESC')); // $select->order('field1 ASC, field2 DESC'); 逗号后面要带个空格
$select->offset(1);
$select->limit(10);
$resultSet = $this->tableGateway->selectWith($select); // ---关联---
// SELECT "table1_alias"."t1_field1" AS "t1_field1", "table1_alias"."t1_field2" AS "t1_field2_alias", "table2_alias"."t2_field1" AS "t2_field1", "table2_alias"."t2_field2" AS "t2_field2", "table3_alias".*
// FROM "album" AS "table1_alias"
// LEFT JOIN "table2" AS "table2_alias" ON "table2_alias"."t1_field1_ref" AND "table1_alias"."t1_field1"
// LEFT JOIN "table3" AS "table3_alias" ON "table3_alias"."t1_field1_ref" AND "table1_alias"."t1_field1"
// WHERE field1=:where1 and field2=:where2
// GROUP BY "t1_field1", "t1_field2"
// ORDER BY "t1_field1" ASC, "t1_field2" DESC
// LIMIT :limit OFFSET :offset
$select = new \Zend\Db\Sql\Select();
$select->from(array('table1_alias'=>'album'));
$select->columns(array('t1_field1','t1_field2_alias'=>'t1_field2'));
$select->where($where);
$select->group(array('t1_field1','t1_field2'));
$select->join(array('table2_alias'=>'table2'), 'table2_alias.t1_field1_ref AND table1_alias.t1_field1', array('t2_field1','t2_field2') ,'LEFT');
$select->join(array('table3_alias'=>'table3'), 'table3_alias.t1_field1_ref AND table1_alias.t1_field1', '*' ,'LEFT');
$select->order(array('t1_field1'=>'ASC','t1_field2'=>'DESC'));
$select->offset(1);
$select->limit(10);
$resultSet = $this->tableGateway->selectWith($select); // ---子查询---
// 执行 case.1,【子查询作为条件】
// SELECT * from pg_user where id in(SELECT id from pg_user);
$selectSubTable = new \Zend\Db\Sql\Select();
$selectSubTable->columns(array('t2_field1'));
$selectSubTable->where($where);
$selectSubTable->from(array('table2_alias'=>'table2'));
$select = new \Zend\Db\Sql\Select();
$select->columns(array('t1_field1','t1_field2_alias'=>'t1_field2'));
$select->where(array('id'=>array($selectSubTable)));
$select->from(array('table1_alias'=>'album'));
$select->limit(10);
$resultSet = $this->tableGateway->selectWith($select); // 执行 case.2,【子查询作为表】子查询出列表,再从列表中查询
// SELECT "sub_table2_alias"."t1_field1" AS "t1_field1", "sub_table2_alias"."t1_field2" AS "t1_field2_alias"
// FROM (SELECT "table2_alias"."t2_field1" AS "t2_field1", "table2_alias"."t2_field2" AS "t2_field2_alias" FROM "table2" AS "table2_alias") AS "sub_table2_alias"
// LIMIT :limit
$selectSubTable = new \Zend\Db\Sql\Select();
$selectSubTable->columns(array('t2_field1','t2_field2_alias'=>'t2_field2'));
$selectSubTable->from(array('table2_alias'=>'table2'));
{ // build $tableGateway
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new \Album\Model\Album());
$tableGateway = new \Zend\Db\TableGateway\TableGateway(array('sub_table2_alias'=>$selectSubTable), $dbAdapter, null, $resultSetPrototype);
}
$select = new \Zend\Db\Sql\Select();
$select->columns(array('t1_field1','t1_field2_alias'=>'t1_field2'));
$select->from(array('sub_table2_alias'=>$selectSubTable));
$select->limit(10);
$resultSet = $tableGateway->selectWith($select); // 执行 case.3,【子查询作为字段】子查询只能返回单条
// SELECT "table1_alias"."t1_field1" AS "t1_field1", "table1_alias"."t1_field2" AS "t1_field2_alias", (SELECT "table2_alias"."t2_field2" AS "t2_field2_alias" FROM "table2" AS "table2_alias") AS "sub_field2_alias"
// FROM "album" AS "table1_alias" LIMIT :limit
$selectSub = new \Zend\Db\Sql\Select();
$selectSub->from(array('table2_alias'=>'table2'));
$selectSub->columns(array('t2_field2_alias'=>'t2_field2')); // 只能有一列
$select = new \Zend\Db\Sql\Select();
$select->from(array('table1_alias'=>'album'));
$select->columns(array('t1_field1','t1_field2_alias'=>'t1_field2','sub_field2_alias'=>$selectSub));
$select->limit(10);
$resultSet = $this->tableGateway->selectWith($select); // ---联合查询---
// ( SELECT "table1_alias"."t1_field1" AS "t1_field1", "table1_alias"."t1_field2" AS "field2_alias" FROM "album" AS "table1_alias" )
// UNION ALL ( SELECT "table2_alias"."t2_field1" AS "t2_field1", "table2_alias"."t2_field2" AS "field2_alias" FROM "table2" AS "table2_alias" )
$select = new \Zend\Db\Sql\Select();
$select->from(array('table1_alias'=>'album'));
$select->columns(array('t1_field1','t1_field2_alias'=>'t1_field2'));
$select1 = new \Zend\Db\Sql\Select();
$select1->from(array('table2_alias'=>'table2'));
$select1->columns(array('t2_field1','t2_field2_alias'=>'t2_field2'));
$select->where($where);
$select->combine($select1,'UNION ALL','');
$resultSet = $this->tableGateway->selectWith($select); // ---统计---
// SELECT "album"."count""("*")" AS "count_total" FROM "album"
$select = $this->tableGateway->getSql()->select();
$select->columns(array('count_total'=>new \Zend\Db\Sql\Expression('count(*)')));
$resultSet = $this->tableGateway->selectWith($select); // ---------结果集---------
// ---单条---
$itemRecord = $resultSet->current();
echo $itemRecord->field1; // ---列表---
$itemRecordList = $resultSet;
foreach ($itemRecordList as $itemRecord){
echo $itemRecord->field1;
}
} /**
* 插入
*/
public function testInsert(){
$where = $this->testWhere();
// ---------添加---------
// case.0
// INSERT INTO "album" ("field1", "field2") VALUES (:field1, :field2)
$data = array(
'field1' =>'field1_value',
'field2' =>'field2_value',
);
$affectedRows = $this->tableGateway->insert($data); // case.1
// INSERT INTO "album" ("field1", "field2") VALUES (:field1, :field2)
$insert = $this->tableGateway->getSql()->insert();
$insert->values($data);
$affectedRows = $this->tableGateway->insertWith($insert); // case.2 查询插入
// INSERT INTO "album" ("des_table_field1", "des_table_field2") SELECT "table1_alias"."field1" AS "field1", "table1_alias"."field2" AS "field2_alias" FROM "table1" AS "table1_alias" WHERE ...
$select = new \Zend\Db\Sql\Select();
$select->columns(array('field1','field2_alias'=>'field2'));
$select->where($where);
$select->from(array('table1_alias'=>'table1')); $insert = $this->tableGateway->getSql()->insert();
$insert->select($select);
$insert->into('album');
$insert->columns(array('des_table_field1','des_table_field2'));
$affectedRows = $this->tableGateway->insertWith($insert);
} /**
* 删除
*/
public function testDelete(){
$where = $this->testWhere();
// ---------删除---------
// case.0
// DELETE FROM "album" WHERE ...
$affectedRows = $this->tableGateway->delete($where); // case.1
// DELETE FROM "album" WHERE ...
$delete = $this->tableGateway->getSql()->delete();
$delete->where($where);
$affectedRows = $this->tableGateway->deleteWith($delete);
} /**
* 修改
*/
public function testUpdate(){
$where = $this->testWhere();
// ---------修改---------
$data = array(
'field1' =>'field1_value',
'field2' =>'field2_value',
);
// case.0
// UPDATE "album" SET "field1" = :field1, "field2" = :field2 WHERE ...
$affectedRows = $this->tableGateway->update($data, $where); // case.1
// UPDATE "album" SET "field1" = :field1, "field2" = :field2 WHERE ...
$update = $this->tableGateway->getSql()->update();
$update->set($data);
$update->where($where);
$affectedRows = $this->tableGateway->updateWith($update);
} }

ZendFramework-2.4 源代码 - 关于MVC - Model层的更多相关文章

  1. ZendFramework-2.4 源代码 - 关于MVC - Model层类图

  2. ZendFramework-2.4 源代码 - 关于MVC - View层 - 视图渲染器、视图插件管理器

    <?php // 1. 视图渲染器 class PhpRenderer implements Renderer, TreeRendererInterface { /** * 插件管理器 */ p ...

  3. ZendFramework-2.4 源代码 - 关于MVC - Controller层

    // 1.控制器管理器 class ServiceManager implements ServiceLocatorInterface { public function __construct(Co ...

  4. ZendFramework-2.4 源代码 - 关于MVC - View层 - 控制器返回值

    <?php class ReturnController extends AbstractActionController { public function returnAction() { ...

  5. ZendFramework-2.4 源代码 - 关于MVC - View层 - 在模板内渲染子模板

    <?php // 方式一: // 1.在模板内直接编写如下内容即可 $viewModel = new ViewModel(); $viewModel->setTemplate('album ...

  6. PHP MVC 中的MODEL层

    Model层,就是MVC模式中的数据处理层,用来进行数据和商业逻辑的装封 三.实现你的Mode层 Model层,就是MVC模式中的数据处理层,用来进行数据和商业逻辑的装封,进行他的设计的时候设计到三个 ...

  7. MVC的Model层中的一些便签

    由于自己重新接触MVC,所以把Model层里的一些标签给记录下来,方便自己的使用. 这些是自己目前试用过的一些,在以后的工作中我会接着补充进去新的内容

  8. MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求

    MVC5中Model层开发数据注解   ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证( ...

  9. 2013/11/22工作随笔-缓存是放在Model层还是放在Controller层

    web网站的典型代码框架就是MVC架构,Model层负责数据获取,Controller层负责逻辑控制,View层则负责展示. 一般数据获取是去mysql中获取数据 但是这里有个问题,我们不会每次请求都 ...

随机推荐

  1. new与malloc区别

    1.new分配内存时会按照数据类型计算需要分配内存的大小,malloc分配内存时是按照指定的大小分配的:2.new不仅分配一段内存,而且会调用构造函数,malloc不会调用构造函数:之前看到过一个题说 ...

  2. F. Gourmet and Banquet(贪心加二分求值)

    题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...

  3. 你一直在用的 Spring Boot Starters 究竟是怎么回事

    Spring Boot 对比 Spring MVC 最大的优点就是使用简单,约定大于配置.不会像之前用 Spring MVC 的时候,时不时被 xml 配置文件搞的晕头转向,冷不防还因为 xml 配置 ...

  4. Default Bearer, Dedicated Bearer... What exactly is bearer ?

    Default Bearer, Dedicated Bearer... What exactly is bearer ?   While trying to get a better understa ...

  5. Java使用Zxing生成、解析二维码工具类

    Zxing是Google提供的关于条码(一维码.二维码)的解析工具,提供了二维码的生成与解析的方法. 1.二维码的生成 (1).将Zxing-core.jar 包加入到classpath下. (2). ...

  6. Sql Server 2012 存储过程的单步调试

    最近在做vb项目的时候,用到了存储过程的调试,现在总结一下发现单步调试存储过程有以下2种方法: 1.这种方法自己已经做过,是可以的,如下: a.如果目标数据库存在存储过程,右击该存储过程-修改,打开存 ...

  7. 基于HttpClient的新版正方教务系统模拟登录及信息获取API

    简介 通过HttpClient获取网页数据源,通过Jsoup解析数据.先模拟登录,再获取信息.模拟浏览器正常操作,封装请求头信息获取SESSIONID.模拟登录成功后切勿断开会话,依赖登录请求得到的C ...

  8. Java集合框架—List

    Collection |--List:元素是有序的,元素可以重复.因为该集合体系有索引. |--ArrayList:底层的数据结构使用的是数组结构.特点:查询速度很快.但是增删稍慢.线程不同步. |- ...

  9. Android 类似360悬浮窗口实现源码

    当我们在手机上安装360安全卫士时,手机屏幕上时刻都会出现一个小浮动窗口,点击该浮动窗口可跳转到安全卫士的操作界面,而且该浮动窗口不受其他activity的覆盖影响仍然可见(多米音乐也有相关的和主界面 ...

  10. 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    参考原文:http://bbs.csdn.net/topics/390121452 项目>属性>配置属性>清单工具>输入和输出>嵌入清单:原来是“是”,改成“否” 如果上 ...