kamandlou/php-query-builder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kamandlou/php-query-builder

Composer 安装命令:

composer require kamandlou/php-query-builder

包简介

A sql query builder for PHP

README 文档

README

PHP SQL query builder using PDO. It's a quick and light library.

Features

  • Easy interface for creating robust queries
  • Supports any database compatible with PDO
  • The ability to build complex SELECT, INSERT, UPDATE & DELETE queries with little code
  • Type hinting for magic methods with code completion in smart IDEs

Installation

Composer

The preferred way to install PHP Query Builder is via composer.

Run this command for installing the PHP Query Builder:

composer require kamandlou/php-query-builder

Make a connection

$pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'password');
$db = DB::connection($pdo);

Set fetch mode

$db = DB::connection($pdo)->setFetchMode(PDO::FETCH_ASSOC);

DB Class Methods

setPrefix(string $prefix)

Set a prefix to be added to all table names.

$db->setPrefix('db');

setSeparator(string $separator)

Set the separator character used when building queries.

$db->setPrefix('db')->setSeparator('_'); // Result: db_tableName

CRUD Query Examples

SELECT
$db->table('users')
    ->select()
    ->where('id', '>', 100)
    ->orderByDesc('id')
    ->get();
$db->table('users')
    ->select('id', 'username')
    ->where('id', '>', 100)
    ->orderByDesc('id')
    ->get();

You can get the pure SQL query with toSql method.

$db->table('users')
    ->select('id', 'username')
    ->where('id', '>', 100)
    ->orderByRand()
    ->toSql();

Select Class Methods

The Select class provides methods for building SQL SELECT queries.

select(...$columns)

Specify columns to select.

$db->select('id', 'name')
// OR
$db->select() // If you don't specify any columns it selects all columns

where($column, $operator, $value)

Add a WHERE condition to the query.

$db->select()->where('age', '>', 18);
  • $column - The column name to compare against.
  • $operator - Comparison operator (>, <, =, <>, etc).
  • $value - The value to compare the column against.

orWhere($column, $operator, $value)

Add an OR WHERE condition to the query.

$db->select()->orWhere('age', '>', 18);
  • $column - The column name to compare against.
  • $operator - Comparison operator (>, <, =, <>, etc).
  • $value - The value to compare the column against.

toSql()

Get the generated SQL query string.

$sql = $db->select()->orWhere('age', '>', 18)->toSql();
echo $sql;

first()

Execute the query and return the first result row.

$user = $db->table('users')->select('id', 'name')->where('id', '=', 5)->first();

get()

Execute the query and return all result rows.

$users = $db->table('users')->select('id', 'name')->where('id', '>', 100)->get();

orderBy(string $column, string $direction = 'ASC')

Set the ORDER BY clause for the query.

$users = $db->table('users')->select('id', 'name', 'age')->orderBy('age', 'DESC')->get();
  • $column - Name of column to sort by.
  • $direction - Sort direction, 'ASC' or 'DESC'.

orderByDesc(string $column)

Set the ORDER BY clause to sort by a column in descending order.

$users = $db->table('users')->select('id', 'name', 'age')->orderByDesc('age')->get();
  • $column - Name of column to sort by.

orderByRand()

Set the ORDER BY to random order.

$users = $db->table('users')->select()->orderByRand()->get();

count()

Get the count of the records.

$count = $db->table('users')->select()
    ->where('id','>',3)
    ->where('id', '<', 100000)
    ->count();
// OR
$count = $db->table('users')->select()->count();

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-05