用Yii2也有一段时间了,发现Yii2 Framework对Database的操作有非常良好的结构和弹性。

接下来介绍三种数据库操作方式。

SQL Command Level:

// Get DB componet of Application
$db = Yii::$app->db; # Get data form SQL execution
$ arrData = $db->createCommand('SELECT * FROM table limit 10')
->queryAll();
print_r($arrData);
Query Builder Level
$arrData = (new \yii\db\Query())
->select(['*'])
->from('table')
->where([])
->limit(10)
->all(Yii::$app->db); // 选择数据库 print_r($arrData);
Active Record (ORM)
$objData = \app\models\ActiveRecordModal::find()
->select(['*'])
->where([])
->limit(10)
->all(); print_r($objData);

那么这三种对区别是什么呢?

1. SQL Command 本身(yii\db\command) 作为Query builder、Active Record对底层,所以需要在createCommand() 之后使用。

2. Active Record(ORM) 一个Object会带有多个成员变量和成员函数导致性能上的损耗。

3. Active Record使用asArray() 可以略过ORM的一些细节上处理,将返回值由对象转为数组,达到Builder Level效能。

Yii2 三层设计模式:SQL Command、Query builder、Active Record(ORM)的更多相关文章

  1. Yii2 数据库Active Record(ORM)

    ACTIVE RECORD(ORM) 参考:http://www.yiiframework.com/doc-2.0/guide-db-active-record.html namespace app\ ...

  2. yii Query Builder (yii 查询构造器) 官方指南翻译

    /**** Query Builder translated by php攻城师 http://blog.csdn.net/phpgcs Preparing Query Builder 准备 Quer ...

  3. RoR - Introduction to Active Record

    Active Record: ORM ( Object-relational Mapping)Bridges the gap between relational databases , which ...

  4. yii2 Query Builder 查询打印sql语句

    $query = new Query(); $query->select('gs.*, g.goods_images, sa.attr_name, sa.is_default, sa.alias ...

  5. Yii2 数据操作Query Builder

    转载地址: http://blog.csdn.net/hzqghost/article/details/44117081 Yii2 数据操作Query Builder 分类: Yii22015-03- ...

  6. [moka同学笔记]Yii2 数据操作Query Builder 2

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  7. [moka同学笔记]Yii2 数据操作Query Builder

    Query Builder [php] view plain copy   $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name ...

  8. Yii2 数据操作Query Builder查询数据

    Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...

  9. SqlKata - 方便好用的 Sql query builder

    SqlKata查询生成器是一个用C# 编写的功能强大的Sql查询生成器.它是安全的,与框架无关.灵感来源于可用的顶级查询生成器,如Laravel Query Builder和 Knex. SqlKat ...

随机推荐

  1. charles 注册码

    感谢@那时纯真 提供的注册码.Windows和Mac通用. 软件去官网下载安装即可. Registered Name:https://zhile.io License Key: 48891cf209c ...

  2. TensorFlow学习之二

    二.常用操作符和基本数学函数 大多数运算符都进行了重载操作,使我们可以快速使用 (+ - * /) 等,但是有一点不好的是使用重载操作符后就不能为每个操作命名了. 1  算术操作符:+ - * / % ...

  3. 通过jquery 获取用户当前所在的城市名称和IP地址

    下面这段JS代码是通过jquery 结合新浪IP地址库和QQip地址库接口获取用户当前所在的城市(省份)名称. 用户当前IP地址等数据.其中当前IP是通过 QQip地址库接口获取,其他数据都是通过 新 ...

  4. window、location、location.href、self、top简单介绍

    1.self:当前窗口对象(如果是在iframe里,则为该框架的窗口对象) 2.top:父窗口对象 3.window:典型情况下,浏览器会为每一个打开的html创建对应的window对象,如果这个文档 ...

  5. bazel build //tensorflow/examples/android:tensorflow_demo报错: fatal error: 'cuda_runtime.h' file not found

    In file included from ./third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:external/eigen_archive/u ...

  6. 原生js实现Base64编码解码

    注:ie10+ var str = window.btoa("liusong"); console.log(str); var s = window.atob("bGl1 ...

  7. 关于@autoreleasepool

    苹果推荐使用场景: 如果你编写的程序不是基于 UI 框架的,比如说命令行工具: 如果你编写的循环中创建了大量的临时对象:(常用) 如果你创建了一个辅助线程. @interface ViewContro ...

  8. 113. Path Sum II 输出每个具体路径

    [抄题]: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the gi ...

  9. [leetcode]10. Regular Expression Matching正则表达式的匹配

    Given an input string (s) and a pattern (p), implement regular expression matching with support for  ...

  10. 50-2018 蓝桥杯省赛 B 组模拟赛(五)

    1.结果填空:矩阵求和 import java.math.BigInteger; import java.util.HashSet; public class Main{ public static ...