美文网首页
Azalea\MysqlModel

Azalea\MysqlModel

作者: Bun_Wong | 来源:发表于2016-09-03 13:37 被阅读67次

    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 )
    
    • 参数
      $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。SQL 中可带占位符,并根据第二个参数 $binds 进行绑定并转义
      $binds - 查询绑定数组,第一个元素绑定 SQL 中第一个占位符,第二个元素绑定 SQL 中第二个占位符,以此类推
      $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]]] )
    
    • 参数
      $table - 表名
      $set - 插入值数组。必须为键值对数组,键为字段名,值为插入值;若批量插入,则为二维数组
      $ignoreErrors - 是否加入 IGNORE 关键字,默认为 false,常用于重复键插入
      $duplicateKeyUpdate - 是否加入 ON DUPLICATE KEY UPDATE 关键字,默认为 false
      $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] )
    
    • 参数
      $table - 表名
      $set - 替换值数组。必须为键值对数组,键为字段名,值为插入值;若批量替换,则为二维数组
      $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]] )
    
    • 参数
      $table - 表名
      $set - 更新值数组。必须为键值对数组,键为字段名,值为插入值
      $where - 条件数组,默认为 null,即 全表 替换
      $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]] )
    
    • 参数
      $table - 表名
      $where - 条件数组,默认为 null,即 全表 删除
      $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');
    

    相关文章

      网友评论

          本文标题:Azalea\MysqlModel

          本文链接:https://www.haomeiwen.com/subject/tpvkettx.html