ORM: 对象-关系映射"(Object/Relational Mapping): 一表一个类,一行一对象,通过实例对象的语法,完成关系型数据库的操作;
ORM 使用对象,封装了数据库操作,因此不涉及SQL语言。开发者只使用面向对象编程,与数据对象直接交互,不用关心底层数据库,运用过面向对象的思想来操作数据对象
对于复杂的查询,ORM 要么是无法表达,要么是性能不如原生的 SQL。
Active Record则是随着ruby on rails的流行而火起来的一种ORM模式,它是把负责持久化的代码也集成到数据对象中
传统的ORM会把数据对象和负责持久化的代码分开;
AR的主要思想还封装了部分业务逻辑,而且它自己负责把自己持久化,也就是说,通常ar更加适用于单表操作。
准备一个 Active Record 实例
将新值赋给 Active Record 的属性
调用 yii\db\ActiveRecord::save() 保存数据到数据库中。
例如:
// 插入新记录
$customer = new Customer();
$customer->name = 'James';
$customer->email = 'james@example.com';
$customer->save();
// 更新已存在的记录
$customer = Customer::findOne(123);
$customer->email = 'james@newexample.com';
$customer->save();
DAO是从系统分层结构,把数据存取操作到集中层;
DAO(Data Access Object) 数据访问对象是一个面向对象的数据库访问接口:driver : 目的便是把和数据库相关的代码封装起来
你主要需要处理纯 SQL 语句和 PHP 数组。因此,这是访问数据库最高效的方法(不同数据库之间的 SQL 语法往往是不同)
AR把自己的$this->data属性的值,赋给一个数组变量
ActiveRecord是调用__set魔术方法,通过操作属性赋值给$this->data数组;
$User = M("User"); // 实例化User对象
// 然后直接给数据对象赋值
$User->name = 'ThinkPHP';
$User->email = 'ThinkPHP@gmail.com';
// 把数据对象添加到数据库
$User->add();
public function __set($name, $value){
# 设置数据对象的值
$this->data[$name] = $value;
}
public function add($data = '', $options = array(), $replace = false)
{
if (empty($data)) {
// 没有传递数据,获取当前数据对象的值
if (!empty($this->data)) {
$data = $this->data;
// 重置数据
$this->data = array();
} else {
$this->error = L('_DATA_TYPE_INVALID_');
return false;
}
}
// 数据处理
$data = $this->_facade($data);
// 分析表达式
$options = $this->_parseOptions($options);
if (false === $this->_before_insert($data, $options)) {
return false;
}
// 写入数据到数据库
$result = $this->db->insert($data, $options, $replace);
if (false !== $result && is_numeric($result)) {
$pk = $this->getPk();
// 增加复合主键支持
if (is_array($pk)) {
return $result;
}
$insertId = $this->getLastInsID();
if ($insertId) {
// 自增主键返回插入ID
$data[$pk] = $insertId;
if (false === $this->_after_insert($data, $options)) {
return false;
}
return $insertId;
}
if (false === $this->_after_insert($data, $options)) {
return false;
}
}
return $result;
}
======================================================
$option 是一个数组,存放sql查询条件的数组; ORM
$model->select($option)
$option:
1: 数字,则按主键=数值来进行查询,id=1
2:字符串,也是按主键来查询,会根据‘,’来分割多个主键的值,用in来查询,id in (...)
3:$option['where'], $option['order']
-------------
$model->where->order->select();
$model->where('id=1');
$model->__call('where','id=1');
$model->option['where']='id=1';
----------------
_parseoption()函数分析sql查询的条件;
array_merge($this->option,$option);
===================================================
配置文件加载:
1.加载ThinkPHP/conf/convention.php
2.加载ThinkPHP/extend/mode 下的模式配置文件
3.加载项目config.php, App/con/config.php
4.加载ThinkPHP/lang/语言包
5.加载tags切面(系统行为),如果模式中array('tags'), App/conf/tags都存在,优先使用模式的extends
6.加载tags切面(应用行为),如果模式中array('tags'), APP/conf/tags 都存在,优先使用模式的
7.加载APP/conf/common.php
8.加载模式别名ThinkPHP/conf/mode(alias)
9.加载APP/conf/alias.php
10.加载Think/conf/debug.php
11.加载APP/conf/debug.php (取决于app_status的值)
=======================================================
$userModel=new UserModel('库名.表名')
如果传了'库名.表名'或'表名',则设置$this->name=‘表名’,否则$this->name=User (截取UserModel)
$this->db()连接数据库;
$this->fields属性为空;DB_FIELDS_CACHE 为false
$this->__construct()--$this->db()--$this->db()--$this->_checkTableInfo()--$this->flush()--$db->getFields()--'show columns from tableNmae' 获取表字段及主键
DB_FIELDS_CACHE=true,则缓存表列名;
========================================================
Model:
自动验证:
'require' => '/\S+/',
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
'currency' => '/^\d+(\.\d+)?$/',
'number' => '/^\d+$/',
'zip' => '/^\d{6}$/',
'integer' => '/^[-\+]?\d+$/',
'double' => '/^[-\+]?\d+(\.\d+)?$/',
protected $_validate=[
array('username','checkUserName','用户名不含**',1,'function'),
array('email','email','email必填'),
array(‘email’,'',’email已被占用‘,1,'unique'),
array('abc','require','abc必填'),
array('gender','男,女',2,'in'),
array('age','18,120','年龄不对',2,'between')
array('repassword','password','两次密码不一致',1,'confirm')
];
--------------------------------------------
自动填充:自动完成
protected $_auto=array(
array('register_time','time',1,'function'),
array('country','CN'),
array('abc','password',1,'field') # 从password读取值赋给abc
);
---------------------------------------------
字段映射:
---------------------------------------------
如何把变量展示到模板?
1,把变量赋给模板引擎,由引擎来存储,$smarty->assign('title',$title);
2,引擎再找模板,把变量与模板混合起来,$smarty->display('a.html');