MysqlModel Mysql 數(shù)據(jù)庫(kù)服務(wù)模塊類
?? 要使用 Azalea\MysqlModel 必須在配置中聲明使用
$config['node-beauty']['mysql'] = 1
?? MysqlModel 構(gòu)造函數(shù)已私有劳秋,無法通過 new 方式實(shí)例化吸申,僅通過 控制器 或 模塊 的
getModel
方法獲得
// in controller-action
$mysqlModel = $this->getModel('mysql');
$data = $mysqlModel->getSqlBuilder()
->from('test')
->orderBy('status')
->query()
->all();
$result = $mysqlModel->insert('test', [ 'foo' => 'bar' ]);
目錄
MysqlSqlBuilder <small>SQL 構(gòu)建類</small>
MysqlResult <small>SQL 查詢結(jié)果類</small>
MysqlQueryResult <small>查詢結(jié)果類</small>
MysqlExecuteResult <small>執(zhí)行結(jié)果類</small>
MysqlModel::escape
轉(zhuǎn)義 SQL 字符串
mixed MysqlModel::escape ( mixed $value )
參數(shù)
$value - 需要轉(zhuǎn)移的字符串或數(shù)組返回值
根據(jù)傳入類型返回
參數(shù)類型 | 返回值類型 |
---|---|
字符串 | 字符串 |
整型 | 字符串 |
浮點(diǎn)型 | 字符串 |
布爾型 | 字符串 "1" /"0"
|
NULL | 字符串 "NULL"
|
數(shù)組 | 數(shù)組 |
- 范例
$mysqlModel->escape("This's\nAzalea.");
// 返回 This\'s\nAzalea.
$mysqlModel->escape([ 'fo"o' => 'ba"r', 'hello', 'world' ]);
// 返回
// [
// fo\"o => ba\"r,
// hello,
// world,
// ]
MysqlModel::query
執(zhí)行 SQL 查詢
MysqlResult MysqlModel::query ( string $sql [, mixed $binds = null [, bool $throwsException = true]] )
- 參數(shù)
$sql - 需要查詢的 SQL。SQL 中可帶占位符丛楚,并根據(jù)第二個(gè)參數(shù)$binds
進(jìn)行綁定并轉(zhuǎn)義
$binds - 查詢綁定數(shù)組,第一個(gè)元素綁定 SQL 中第一個(gè)占位符憔辫,第二個(gè)元素綁定 SQL 中第二個(gè)占位符趣些,以此類推
$throwsException - 是否拋出異常,默認(rèn)為true
占位符 | 描述 |
---|---|
?? |
SQL 關(guān)鍵字贰您,如表名坏平、字段名拢操,轉(zhuǎn)以后會(huì)加上 "\ "` |
? |
SQL 值 如果是數(shù)字鍵,則執(zhí)行 escape 舶替;如果是字符串鍵令境,則執(zhí)行 eacape 后用 = 連接 |
- 返回值
查詢結(jié)果對(duì)象
結(jié)果類型 | 描述 |
---|---|
MysqlResult | 默認(rèn)結(jié)果,查詢出錯(cuò)時(shí)返回($throwsException = false ) |
MysqlQueryResult | 查詢結(jié)果顾瞪,如 DQL舔庶,SELECT ,SHOW 等玲昧,繼承于 MysqlResult
|
MysqlExecuteResult | 執(zhí)行結(jié)果栖茉,如 DDL、DML孵延,INSERT 吕漂,UPDATE ,DELETE 等尘应,繼承于 MysqlResult
|
- 范例
$result = $mysqlModel->query('SELECT ?? FROM ?? WHERE ? AND ?? = ?',
[ 'name', 'table', 'foo' => 'bar', 'hello', true ]);
// 執(zhí)行查詢 SQL
// SELECT `name` FROM `table` WHERE `foo` = "bar" AND `hello` = "1"
// 執(zhí)行成功則返回 MysqlQueryResult 對(duì)象
MysqlModel::getQueries
獲取查詢 SQL 歷史記錄
mixed MysqlModel::getQueries ( void )
參數(shù)
無返回值
查詢記錄數(shù)組范例
$mysqlModel->getQueries();
// 返回執(zhí)行過的 SQL 數(shù)組
// [
// SELECT * FROM `test`,
// INSERT ...
// ]
MysqlModel::insert
執(zhí)行插入查詢
MysqlResult MysqlModel::insert ( string $table, array $set [, bool $ignoreErrors = false [, bool $duplicateKeyUpdate = false [, bool $throwsException = true]]] )
參數(shù)
$table - 表名
$set - 插入值數(shù)組惶凝。必須為鍵值對(duì)數(shù)組,鍵為字段名犬钢,值為插入值苍鲜;若批量插入,則為二維數(shù)組
$ignoreErrors - 是否加入IGNORE
關(guān)鍵字玷犹,默認(rèn)為false
混滔,常用于重復(fù)鍵插入
$duplicateKeyUpdate - 是否加入ON DUPLICATE KEY UPDATE
關(guān)鍵字,默認(rèn)為false
$throwsException - 是否拋出異常歹颓,默認(rèn)為true
返回值
執(zhí)行結(jié)果對(duì)象范例
$mysqlModel->insert('table', [ 'foo' => 'bar', 'hello' => true ], true, true);
// 執(zhí)行插入 SQL
// INSERT IGNORE INTO `table` (`foo`,`hello`) VALUES ("bar","1") ON DUPLICATE KEY UPDATE `foo` = VALUES(`foo`),`hello` = VALUES(`hello`)
MysqlModel::replace
執(zhí)行替換查詢
MysqlResult MysqlModel::replace ( string $table, array $set [, bool $throwsException = true] )
參數(shù)
$table - 表名
$set - 替換值數(shù)組坯屿。必須為鍵值對(duì)數(shù)組,鍵為字段名巍扛,值為插入值领跛;若批量替換,則為二維數(shù)組
$throwsException - 是否拋出異常撤奸,默認(rèn)為true
返回值
執(zhí)行結(jié)果對(duì)象范例
$mysqlModel->replace('table', [ 'foo' => 'bar', 'hello' => true ]);
// 執(zhí)行替換 SQL
// REPLACE INTO `table` (`foo`,`hello`) VALUES ("bar","1")
MysqlModel::update
執(zhí)行更新查詢
MysqlResult MysqlModel::update ( string $table, array $set [, mixed $where = null [, bool $throwsException = true]] )
參數(shù)
$table - 表名
$set - 更新值數(shù)組吠昭。必須為鍵值對(duì)數(shù)組,鍵為字段名胧瓜,值為插入值
$where - 條件數(shù)組矢棚,默認(rèn)為null
,即 全表 替換
$throwsException - 是否拋出異常贷痪,默認(rèn)為true
返回值
執(zhí)行結(jié)果對(duì)象范例
$mysqlModel->update('table', [ 'foo' => 'bar', 'hello' => true ], [ 'status IN' => [ 1, 2, 3 ]]);
// 執(zhí)行更新 SQL
// UPDATE `table` SET `foo` = "bar",`hello` = "1" WHERE `status` IN ("1","2","3")
MysqlModel::delete
執(zhí)行刪除查詢
MysqlResult MysqlModel::delete ( string $table [, mixed $where = null [, bool $throwsException = true]] )
參數(shù)
$table - 表名
$where - 條件數(shù)組幻妓,默認(rèn)為null
,即 全表 刪除
$throwsException - 是否拋出異常,默認(rèn)為true
返回值
執(zhí)行結(jié)果對(duì)象范例
$mysqlModel->delete('table', [ 'foo' => 'bar', 'status IN' => [ 1, 2, 3 ]]);
// 執(zhí)行刪除 SQL
// DELETE FROM `table` WHERE `foo` = "bar" AND `status` IN ("1","2","3")
MysqlModel::getSqlBuilder
獲取 SQL 構(gòu)建對(duì)象
MysqlSqlBuilder MysqlModel::getSqlBuilder ( void )
參數(shù)
無范例
$mysqlModel->getSqlBuilder()
->from('test')
->orderBy('status');