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 = '[email protected]';
$customer->save();
// 更新已存在的記錄
$customer = Customer::findOne(123);
$customer->email = '[email protected]';
$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 = '[email protected]';
// 把數據對象添加到數據庫
$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');