Azalea\MysqlModel

2016-09-03  本文已影响67人  Bun_Wong

MysqlModel Mysql 数据库服务模块类

⚠️ 要使用 Azalea\MysqlModel 必须在配置中声明使用 $config['node-beauty']['mysql'] = 1

⚠️ MysqlModel 构造函数已私有,无法通过 new 方式实例化,仅通过 控制器模块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 构建类</small>
MysqlResult <small>SQL 查询结果类</small>
MysqlQueryResult <small>查询结果类</small>
MysqlExecuteResult <small>执行结果类</small>

MysqlModel::escape


转义 SQL 字符串

mixed MysqlModel::escape ( mixed $value )
参数类型 返回值类型
字符串 字符串
整型 字符串
浮点型 字符串
布尔型 字符串 "1"/"0"
NULL 字符串 "NULL"
数组 数组
$mysqlModel->escape("This's\nAzalea.");
// 返回 This\'s\nAzalea.
$mysqlModel->escape([ 'fo"o' => 'ba"r', 'hello', 'world' ]);
// 返回
// [
//   fo\"o => ba\"r,
//   hello,
//   world,
// ]

MysqlModel::query


执行 SQL 查询

MysqlResult MysqlModel::query ( string $sql [, mixed $binds = null [, bool $throwsException = true]] )
占位符 描述
?? SQL 关键字,如表名、字段名,转以后会加上 "\"`
? SQL 值
如果是数字键,则执行 escape
如果是字符串键,则执行 eacape 后用 = 连接
结果类型 描述
MysqlResult 默认结果,查询出错时返回($throwsException = false
MysqlQueryResult 查询结果,如 DQL,SELECTSHOW 等,继承于 MysqlResult
MysqlExecuteResult 执行结果,如 DDL、DML,INSERTUPDATEDELETE 等,继承于 MysqlResult
$result = $mysqlModel->query('SELECT ?? FROM ?? WHERE ? AND ?? = ?',
      [ 'name', 'table', 'foo' => 'bar', 'hello', true ]);
// 执行查询 SQL
// SELECT `name` FROM `table` WHERE `foo` = "bar" AND `hello` = "1"
// 执行成功则返回 MysqlQueryResult 对象

MysqlModel::getQueries


获取查询 SQL 历史记录

mixed MysqlModel::getQueries ( void )
$mysqlModel->getQueries();
// 返回执行过的 SQL 数组
// [
//   SELECT * FROM `test`,
//   INSERT ...
// ]

MysqlModel::insert


执行插入查询

MysqlResult MysqlModel::insert ( string $table, array $set [, bool $ignoreErrors = false [, bool $duplicateKeyUpdate = false [, bool $throwsException = true]]] )
$mysqlModel->insert('table', [ 'foo' => 'bar', 'hello' => true ], true, true);
// 执行插入 SQL
// INSERT IGNORE INTO `table` (`foo`,`hello`) VALUES ("bar","1") ON DUPLICATE KEY UPDATE `foo` = VALUES(`foo`),`hello` = VALUES(`hello`)

MysqlModel::replace


执行替换查询

MysqlResult MysqlModel::replace ( string $table, array $set [, bool $throwsException = true] )
$mysqlModel->replace('table', [ 'foo' => 'bar', 'hello' => true ]);
// 执行替换 SQL
// REPLACE INTO `table` (`foo`,`hello`) VALUES ("bar","1")

MysqlModel::update


执行更新查询

MysqlResult MysqlModel::update ( string $table, array $set [, mixed $where = null [, bool $throwsException = true]] )
$mysqlModel->update('table', [ 'foo' => 'bar', 'hello' => true ], [ 'status IN' => [ 1, 2, 3 ]]);
// 执行更新 SQL
// UPDATE `table` SET `foo` = "bar",`hello` = "1" WHERE `status` IN ("1","2","3")

MysqlModel::delete


执行删除查询

MysqlResult MysqlModel::delete ( string $table [, mixed $where = null [, bool $throwsException = true]] )
$mysqlModel->delete('table', [ 'foo' => 'bar', 'status IN' => [ 1, 2, 3 ]]);
// 执行删除 SQL
// DELETE FROM `table` WHERE `foo` = "bar" AND `status` IN ("1","2","3")

MysqlModel::getSqlBuilder


获取 SQL 构建对象

MysqlSqlBuilder MysqlModel::getSqlBuilder ( void )
$mysqlModel->getSqlBuilder()
      ->from('test')
      ->orderBy('status');
上一篇 下一篇

猜你喜欢

热点阅读