inglar/sql-builder

1.0.3 2017-01-27 15:47 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:52:44 UTC


README

Composer

打开命令控制台,进入您的项目目录并执行以下命令以下载此包的最新稳定版本

$ composer require inglar/sql-builder

此命令要求您全局安装Composer,如Composer文档中的安装章节所述。

支持的适配器

SqlBuilder支持以下数据库适配器

  • MySQL(指定mysql
  • PostgreSQL(指定pgsql

使用方法

简单的选择查询

$builder = new SqlBuilder('pgsql');
$select = $builder->select()
    ->column('*')
    ->from('table')
    ->where('id = :id')
    ->bindParam(':id', 123);
 
echo $select;
print_r($select->getBindParams());

以上示例将输出

SELECT * FROM "table" WHERE id = :id
 
Array
(
    [:id] => 123
)

带有连接的选择查询

$builder = new SqlBuilder('pgsql');
$select = $builder->select()
    ->column('*')
    ->from('table')
    ->join($builder->join('table2', "table2.user_id = table.id")
    ->where('id = :id')
    ->bindParam(':id', 123);
 
echo $select;
print_r($select->getBindParams());

以上示例将输出

SELECT * FROM "table" JOIN "table2" ON table2.user_id = table.id WHERE id = :id
 
Array
(
    [:id] => 123
)