sqltranslator / sql-translator
Data Model, SQL Maker
v1.20
2017-11-02 09:00 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2025-01-14 11:38:16 UTC
README
SQL生成助手!
更新日志
- 2016-08-15 创建项目
- 2016-08-30 项目更新--修复了不能调用的BUG
- 2016-10-18 引入PDO,完善功能。
- 2016-10-19 配置文件外部导入。
- 2017-01-19 查询字段格式统一,新增插入唯一时更新功能
- 2017-04-11 新增行锁接口 --
#调用方式 ###查询
$model = (new \SqlTranslator\Database())->config('mysql://root:#PWD@127.0.0.1:3306/demo')->pick('pdo'); $sql = $model->select() ->from(['a' => 'jst_book'], ['id', 'a.name', 'n' => '#NOW()']) ->joinLeft(['b' => 'jst_book_detail'], 'a.id = b.id', ['b.detail', 'b.cconte', 's' => '#NOW()']) ->where('a.id=1')->lock(); $result = $model->fetchAll($sql); var_dump($result);exit; exit;
###开启事务
$model = (new \SqlTranslator\Database())->config('mysql://root:#PWD@127.0.0.1:3306/demo')->pick('pdo'); $translator = new \SqlTranslator\SqlTranslator(); try { $model->beginTransaction(); $sql = $model->select() ->from(['a' => 'jst_book'], ['id', 'a.name', 'n' => '#NOW()']) ->joinLeft(['b' => 'jst_book_detail'], 'a.id = b.id', ['b.detail', 'b.cconte', 's' => '#NOW()']) ->where('a.id=1')->lock(); $result = $model->fetchAll($sql); $model->commit(); return $oid; } catch (\Exception $e) { $model->rollBack(); return false; } var_dump($result);exit; exit;
###新增
$insert = $model->insert() ->into( 'table', [ 'name', 'phone', 'type', 'price', 'price_type', 'order_count', ] ) ->values( [ $params['name'], $params['phone'], $params['type'], (float)$params['price'], $params['price_type'], 0, ] ) ->duplicate(['order_count' => 1, 'name' => $params['name']]); $result = $model->query($sql); exit;
###修改
$update = $this->_db_translator->update ->set($classname::tableName(), $params) ->where('id=?', $id);
###删除
$delete = $model->delete() ->from('table') ->where('id=?', $id);