PHPackages                             musicman3/cruder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Admin Panels](/categories/admin)
4. /
5. musicman3/cruder

ActiveLibrary[Admin Panels](/categories/admin)

musicman3/cruder
================

CRUD Query Builder with PDO

v1.0.23(2mo ago)0425Apache-2.0PHPPHP ^8.2

Since Apr 13Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/musicman3/Cruder)[ Packagist](https://packagist.org/packages/musicman3/cruder)[ RSS](/packages/musicman3-cruder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)DependenciesVersions (37)Used By (0)

Cruder Query Builder
====================

[](#cruder-query-builder)

### Project installation:

[](#project-installation)

`composer require musicman3/cruder`

### System requirements:

[](#system-requirements)

- OS Unix, Linux or Windows
- Apache Web Server &gt;= 2.4 or Nginx &gt;= 1.17
- PHP &gt;= 8.4
- MySQL || MariaDB || PostgreSQL || SQLite

### PHP extensions and settings:

[](#php-extensions-and-settings)

- pdo\_mysql (for MySQL or MariaDB)
- pdo\_pgsql (for PostgreSQL)
- pdo\_sqlite (for SQLite)

The Cruder Project is a CRUD system for working with databases based on the Query Builder principle and using PDO. This project is primarily developed for the eMarket project:

Cruder currently supports MySQL/MariaDB, Postgree, and SQLite databases. The syntax is the same for all databases, making it easy to switch between different database types on the fly.

At the same time, the library is extracted into a separate project to allow anyone who likes Cruder to use it in their own projects.

The main advantages of this project are the small size of the library and its good performance. Additionally, Cruder initially checks all outgoing data for XSS injections. Since we use PDO, this allows us to eliminate SQL injections through built-in methods.

To start using Cruder, you need to initialize database settings. After initialization, you can perform CRUD operations. Upon completion of the work, you need to close the database connection. Here's an example of how it looks:

```
use \Cruder\Db;

// DB settings

Db::config(
    [
        'mysql' =>
        [
            'db_driver' => 'mysql', // pgsql, sqlite
            'db_server' => 'localhost', // optional, not required for sqlite
            'db_name' => 'my_base',
            'db_username' => 'root',
            'db_password' => 'my_password',
            'db_prefix' => 'emkt_',
            'db_port' => '3306', // optional, not required for sqlite
            'db_family' => 'innodb', // myisam, only for MySQL or empty
            'db_charset' => 'utf8mb4', // only for MySQL or empty
            'db_collate' => 'utf8mb4_unicode_ci', // only for MySQL or empty
            'db_error_url' => '/my_error_page/?error_message=', // optional
            'db_path' => 'localhost/storage/databases/sqlite.db3' // optional, path to SQLite DB
        ]
    ]
);

Db::use('mysql')->transactions('on'); //Use MySQL and Transactions On
// Db::use('mysql'); //Use MySQL without transactions

// Here we perform various actions that you will need for your project.
Db::connect()->read('my_table')
                ->selectAssoc('id')
                ->where('order >=', 5)
                ->orderByDesc('id')
                ->save();

// Close DB connect
Db::close();
```

There are various methods for working with a database. All of them are documented using PHPDoc and PHPDoc tags according to PSR-5 and PSR-19 standards. A call chain is used when forming a query. Here's an example of what it looks like:

```
// Create (INSERT INTO)
Db::connect()
         ->create('my_table')
         ->set('id', 10)
         ->set('order', 5)
         ->set('text', 'This is my text')
         ->save();

// Read (SELECT)
$id = Db::connect()
                ->read('my_table')
                ->selectAssoc('id')
                ->where('order >=', 5)
                ->orderByDesc('id')
                ->save();

// Read with use LEFT JOIN
$data = Db::connect()
                ->read('customers')
                ->selectAssoc('customers.customer_id, customers.first_name, orders.amount')
                ->leftJoin('orders')
                ->on('customers.customer_id =', 'orders.customer')
                ->where('orders.amount >=', '500')
                ->save();

// Update
Db::connect()
         ->update('my_table')
         ->set('text', 'This is my new text')
         ->where('id =', 10)
         ->or('order >=' 5)
         ->save();

// Delete
Db::connect()
         ->delete('my_table')
         ->where('id =', 10)
         ->save();

// use DB-functions -> for example YEAR(date_created)
$data = Db::connect()
                ->read('my_table')
                ->selectAssoc('id, name, {{YEAR->date_created}}')
                ->where('{{YEAR->date_created}} =', '2021-04-21 20:38:40')
                ->orderByDesc('id')
                ->save();

// DB Install
Db::connect()->dbInstall('/full_path_to_db_file/db.sql', 'db_prefix');

// DROP TABLE
Db::connect()->drop('my_table')->save();

// Debug
Db::connect()
         ->create('my_table')
         ->set('id', 10)
         ->set('order', 5)
         ->set('text', 'This is my text')
         ->debug();

//Transactions On/Off
Db::transactions('off'); //Transactions Off

Db::connect()
         ->create('my_table')
         ->set('id', 10)
         ->set('order', 5)
         ->set('text', 'This is my text')
         ->debug();

Db::transactions('on'); //Transactions On
```

If you need to connect to another database, you must specify its settings and then return the previous settings after you have finished working with this database. This allows one project to use unlimited connections to different databases located on different servers.

```
use \Cruder\Db;

Db::config(
    [
        'mysql' =>
        [
            'db_driver' => 'mysql', // pgsql, sqlite
            'db_server' => 'localhost', // optional, not required for sqlite
            'db_name' => 'my_base',
            'db_username' => 'root',
            'db_password' => 'my_password',
            'db_prefix' => 'emkt_',
            'db_port' => '3306', // optional, not required for sqlite
            'db_family' => 'innodb', // myisam, only for MySQL or empty
            'db_charset' => 'utf8mb4', // only for MySQL or empty
            'db_collate' => 'utf8mb4_unicode_ci' // only for MySQL or empty
        ],
        'sqlite' =>
        [
            'db_driver' => 'sqlite',
            'db_name' => 'my_base',
            'db_username' => 'root',
            'db_password' => 'my_password',
            'db_prefix' => 'emkt_',
            'db_path' => 'localhost/storage/databases/sqlite.db3'
        ]
    ]
);

//---------------------------------------------------------- MySQL

// MySQL DB settings
Db::use('mysql')->transactions('on'); //Use MySQL and Transactions On

// We execute queries to the master database
Db::connect()
         ->create('my_table')
         ->set('id', 10)
         ->set('order', 5)
         ->set('text', 'This is my text')
         ->save();

//---------------------------------------------------------- SQLite

// SQLite DB settings
Db::use('sqlite')->transactions('on'); //Use SQLite and Transactions On

//We execute queries to the slave database
Db::connect()
         ->create('my_table')
         ->set('id', 10)
         ->set('order', 5)
         ->set('text', 'This is my text')
         ->save();

//---------------------------------------------------------- MySQL

// MySQL DB settings
Db::use('mysql')->transactions('on'); //Use MySQL and Transactions On

// We execute queries to the master database
$id = Db::connect()
                ->read('my_table')
                ->selectAssoc('id')
                ->where('order >=', 5)
                ->orderByDesc('id')
                ->save();

// Close DB connect
Db::close();
```

Get current Cruder settings:

```
Db::get()
```

A list of available database functions used in SQL queries (the basic functionality is identical to their MySQL counterparts). These functions operate identically across all supported databases, allowing you to perform specific actions within the SQL query. This reduces the burden on subsequent PHP processing in your project.

```
YEAR - similar to the YEAR(datetime) function in MySQL
MONTH - similar to the MONTH(datetime) function in MySQL
DAYOFWEEK - similar to the DAYOFWEEK(datetime) function in MySQL
DAY - similar to the DAY(datetime) function in MySQL
DAYOFYEAR - similar to the DAYOFYEAR(datetime) function in MySQL
QUARTER - similar to the QUARTER(datetime) function in MySQL
HOUR - similar to the HOUR(datetime) function in MySQL
UNIX_TIMESTAMP - similar to the UNIX_TIMESTAMP(datetime) function in MySQL
LIKE - similar to the LIKE function in MySQL
CAST AS CHAR - similar to the CAST(value AS CHAR) function in MySQL
MIN - similar to the MIN(value) function in MySQL
MAX - similar to the MAX(value) function in MySQL
COUNT - similar to the COUNT(column) function in MySQL

-----------------------------------------------------------------------
These functions in Cruder are identical for MySQL, Postgree and SQLite,
so when changing the database on the fly, the result remains the same.
-----------------------------------------------------------------------

Syntax: {{YEAR->date_created}} - YEAR (function name), date_created (function argument)

```

Example

```
$data = Db::connect()
                ->read('my_table')
                ->selectAssoc('id, name, {{YEAR->date_created}}')
                ->where('{{YEAR->date_created}} =', '2021-04-21 20:38:40')
                ->orderByDesc('id')
                ->save();
```

Using your own syntax to work with database functions allows you to use multiple types of databases simultaneously. For example, you can use MySQL or Postgres. New functions can always be added through the pattern located in the database adapter section. For MySQL, this pattern is located in `Mysql/DbFunctions->pattern()`.

All available methods can be viewed in the files CrudInterface.php or by viewing the description of these methods using tooltips in your IDE.

```
create(string $table) - INSERT INTO
read(string $table) - SELECT
update(string $table) - UPDATE
delete(string $table) - DELETE FROM
readDistinct(string $table) - SELECT DISTINCT
drop(string $table) - DROP TABLE
---------------------------------------
set(string $identificator, mixed $value) - SET
where(string $identificator, mixed $value) - WHERE
and(string $identificator, mixed $value) - AND
or(string $identificator, mixed $value) - OR
on(string $identificator, mixed $value) - ON
using(string $identificator) - USING
as(string $identificator) - AS
limit(mixed $offset, mixed $limit) - LIMIT
offset(mixed $offset) - OFFSET
---------------------------------------
groupBy(string $identificator) - GROUP BY
orderBy(string $identificator) - ORDER BY
orderByDesc(string $identificator) - ORDER BY identificator DESC
orderByAsc(string $identificator) - ORDER BY identificator ASC
innerJoin(string $identificator) - INNER JOIN
leftJoin(string $identificator) - LEFT JOIN
----------------------------------------
Any other operator that you can specify yourself
operator(string $operator, string $identificator, mixed $value)
----------------------------------------
selectAssoc(string $identificator) - Get associated array
selectIndex(string $identificator) - Get an indexed array
selectValue(string $identificator) - Get value
selectObj(string $identificator) - Get object
selectColCount(string $identificator) - Count the number of columns
selectRowCount(string $identificator) - Count the number of rows
lastInsertId() - Last Insert ID
----------------------------------------
save() - Query Termination Operator. Terminates a query chain.
debug() - If set instead of save(), the SQL query string will be output
to the browser and then the save() method will be executed.
----------------------------------------
dbInstall(string $path, string $db_prefix = 'emkt_') - Install DB-file (.sql)
exec(string $data) - PDO exec() operator
```

### PHP Standards Recommendations Used:

[](#php-standards-recommendations-used)

- PSR-1 (Basic Coding Standard)
- PSR-4 (Autoloading Standard)
- PSR-5 (PHPDoc Standard)
- PSR-12 (Extended Coding Style Guide)
- PSR-19 (PHPDoc tags)

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance88

Actively maintained with recent releases

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~30 days

Recently: every ~38 days

Total

36

Last Release

60d ago

PHP version history (2 changes)v1.0.0-alphaPHP ^8.2

v1.0.17PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/d567df144c1c5113b5c5b69636f5dfa9ad0bc9d8ed70448b68c13ab486e64549?d=identicon)[musicman3](/maintainers/musicman3)

---

Top Contributors

[![musicman3](https://avatars.githubusercontent.com/u/36889207?v=4)](https://github.com/musicman3 "musicman3 (135 commits)")

---

Tags

crudinnodbmyisammysqlpdopostgreepostgresquery-buildersqlitesqlite3

### Embed Badge

![Health badge](/badges/musicman3-cruder/health.svg)

```
[![Health](https://phpackages.com/badges/musicman3-cruder/health.svg)](https://phpackages.com/packages/musicman3-cruder)
```

###  Alternatives

[jeroennoten/laravel-adminlte

Easy AdminLTE integration with Laravel

4.0k4.8M43](/packages/jeroennoten-laravel-adminlte)[dmstr/yii2-adminlte-asset

AdminLTE backend theme asset bundle for Yii 2.0 Framework

1.1k1.8M67](/packages/dmstr-yii2-adminlte-asset)[dwij/laraadmin

LaraAdmin is a Open source Laravel Admin Panel / CMS which can be used as Admin Backend, Data Management Tool or CRM boilerplate for Laravel with features like CRUD Generation, Module Manager, Media, Menus, Backups and much more

1.6k68.7k](/packages/dwij-laraadmin)[filament/spatie-laravel-media-library-plugin

Filament support for `spatie/laravel-medialibrary`.

1764.8M125](/packages/filament-spatie-laravel-media-library-plugin)[bezhansalleh/filament-exceptions

A Simple &amp; Beautiful Pluggable Exception Viewer for FilamentPHP's Admin Panel

193195.9k13](/packages/bezhansalleh-filament-exceptions)[filament/infolists

Easily add beautiful read-only infolists to any Livewire component.

1220.8M36](/packages/filament-infolists)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
